|
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.
|
|
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 .
|
Ask your Facebook Friends
|
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.
|
|
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 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, guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do.
What I'm doing is this. I have a list, A , and I have a function f which takes an item and returns a list. I can...
Started by Steve Cooper on
, 9 posts
by 9 people.
Answer Snippets (Read the full thread at stackoverflow):
Subs = [] map(subs.extend, (os.listdir(d) for d flatten(listOfLists): return list....
Temp"] subs = list(itertools.chain(*[os.listdir(d) for d in dirs])) print subs
itertools.chain() returns an iterator, hence the passing to list() .
|
|
I'm a python newb and am having trouble groking nested list comprehensions. I'm trying to write some code to read in a file and construct a list for each character for each line.
so if the file contains
xxxcd cdcdjkhjasld asdasdxasda
The resulting list...
Started by shsteimer on
, 8 posts
by 8 people.
Answer Snippets (Read the full thread at stackoverflow):
Data = [list(line.strip().upper()) for line in open(file,'r')]
Here is one level of list()])
But we can do the whole thing on one go:
f = open(file, 'rt') data = [list(line.strip().upper()) for line in f]
This is using list....
|
|
I have list of book titles and I have to list chapters like this.
Title 1 Chapter1 Chapter2 Chapter3 Chapter4 Title 2 Chapter1 Chapter2
So, I have list inside a list. I can get list of books (and titles) but when i reference their chapters i get nothing...
Started by Ante on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Whether the entries are there in the sub-list.
|