Skip to content
Advertisement

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 error. Answer You can try

namedtuple._source not working in python 3.7

I’ve tried with this code But when I run it it says that there is no attribute _source (for t and therefore also for i). Has it been eliminated since 3.6? Answer Yes, as stated here, the attribute _source has been removed from namedtuples in Python 3.7. Changed in version 3.7: Remove the verbose parameter and the _source attribute.

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 handled by generator and/or for expressions,

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 for lists, tuples, sets

Advertisement