Skip to content

Tag: collections

python: get percentage from collection counter

I have two lists like the following: I have tried to get the counts of elements in the first list based on a condition: the output is: instead of counts, I would like to get their percentage like the following I have tried adding Counter(100/([a for a,b in zip(l1,l2) if b==’f’])), but I get an err…

Python LINQ like methods

As new to Python i really miss LINQ methods. I’ve found this and this questions, which helped me a lot to understand how Python enumerables and generators work. But sill, I want to use good old methods like Select, SelectMany, First, Last, Group, Distinct and so on. I understand, that all cases can be h…

Is there a short contains function for lists?

Given a list xs and a value item, how can I check whether xs contains item (i.e., if any of the elements of xs is equal to item)? Is there something like xs.contains(item)? For performance considerations, see Fastest way to check if a value exists in a list. Answer Use: Also, inverse operation: It works fine …