|
The question is confusing, but it is much more clear as described in the following codes:
List<List<T>> listOfList; // add three lists of List<T> to listOfList, for example /* listOfList = new { { 1, 2, 3}, // list 1 of 1, 3, and 3 {...
Started by David.Chu.ca on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Use the SelectMany extension method
list = listOfList.SelectMany(x => x).ToList();
Do you mean List<int>() { 3, 4 }, new List<int>() { 5, 6 } }; var list = new List<int> { 9, 9, 9 = from list in listOfList from....
|
|
Imagine the following list:
List<List<List<String>>> listRoot = new List<List<List<String>>>();
I want to count the elements of the first and the second list and return the accumulated value:
int iFirstListCounter =...
Started by timo2oo8 on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
ListRoot.SelectMany(l => l.SelectMany(li => li)).Count()
int totalCount = listRoot.Sum(x => x.Count + x.Sum(y => y.Count));
This should do it:
int firstListCounter = listRoot.Sum(f => f.Count); int secondListCount = listRoot.Sum(f =>... .
|
|
Lest say you want the last element of a python list: what is the difference between
myList[-1:][0]
and
myList[len(myList)-1]
I thought there was no difference but then I tried this
>>> list = [0] >>> list[-1:][0] 0 >>> list[...
Started by c0m4 on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
To get the same behaviour as ....
list[-1:] creates a new list.
[len(list)-1] is reference to last object.
So [-1:][0] modifies the new list.
If you use slicing [-1:], the returned list is a shallow-copy, not reference.
|
Ask your Facebook Friends
|
Original question: Can someone tell me how to use "slice lists" and the "ellipsis"? When are they useful? Thanks.
Here's what the language definition says about "slice_list" and "ellipsis"; Alex Martelli's answer points out their origin, which is not ...
Started by behindthefall on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
L = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Indexing into a list:
l[0] >>>....
Here goes list slicing:
I hope you know that list indeces begin at 0.
Not too sure about ellipsis, so I will not address that, lest I give you a bad answer .
|
|
I have a List<List<int>> . I would like to convert it into a List<int> where each int is unique. I was wondering if anyone had an elegant solution to this using LINQ.
I would like to be able to use the Union method but it creates a new...
Answer Snippets (Read the full thread at stackoverflow):
How about:
HashSet<int> set = new HashSet<int>(); foreach (List<int> list in listOfLists) { set.UnionWith(list); } return set.ToList();
List allInts = new List();
foreach(List<int> list in listOfLists....
|
|
Hi,
How shall sort this in ASC order?
I got List<List<UInt32>> full of records of List<UInt32> and I want those records to be sorted according to the first column in each record - the column is UInt32 number.
So:
I have List of:
new ...
Started by Skuta on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
This way it works for any IEnumerable<T>.
First() instead of accessing the 0 element in the list.
|
|
I have a list of lists, and I need to find the longest one of them. If there are more than one with the same length it's the same which it returns. Thanks.
Started by misterfixit on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Here is a general predicate that scans a list to find a single member defined by a given goal now is define a predicate get_longer_list(L1, L2, L) , and use it instead of get_bigger_number/3, you should try to avoid calculating the length....
|
|
Hi,
How do I sort List<List<string>> according to number of fields?
The list got structure like
a|b|c|d|eeee|rere|ewew| ewqewq|ewew| ewew|ewewewewew|
By Sort I'd like to do it according to numbers of blockes (asc/desc)
EDIT
the <list<...
Started by Skuta on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
Var sorted * x.Count.CompareTo(y.Count));....
If you don't need to sort "in place" you can use LINQ to give you a new sorted, list.
To create a list that's sorted by the number of elements within a particular string in another list.
|
|
I've got an object of type A which consists of a list of objects of type B :
class A { list<B> Alist;} class B { string C; string D;}
In my program I have a list of A objects:
list<A> listOfA = computeAList();
and I would like to select all...
Started by Chau on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
The query expression equivalent of this is:
var query = from a in computeAList() from b in a.Alist select b.C;
For the sake of completeness, the other answers in this thread are variations on the same... .
Ybo's answer would have been my first response too.
|
|
What is the simplest and reasonably efficient way to slice a list into a list of the sliced sub-list sections for arbitrary length sub lists.
For example, if our source list is:
input = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... ]
And our sub list length is 3 then...
Started by James Austin on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Since you don't define what might happened to the final element of the new list when the number; list(grouper(2, [1,2,3,4,5,6,7])) [(1, 2), (3, 4), (5, 6), (7, None)]
I like SilentGhost's solution] for i in range(n)]) group....
Of a chunk.
|