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
Tag: collections
How to count the same (identical) titles in a row
Good day everyone. I’m trying to count title of soccer team in list of dictionaries. There are 10 (sometimes 20) games in list with data. So, home_team in list – looks like: {Team 1, … }, {Team 1, … }, {Team 2, … }, {Team 2, … }, {Team 1, … }, {Team 2, … }, {Team 2, … },
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 dictionary counter positions change
So i’m trying to count the most repeated values in an text file. By using the Counter method it returns exaclty what im looking for file.txt script.py OUTPUT But as you can see the output of the final array is not in the same order as the dictionary (value should be in descending order) I am expecting an output like
How to turn a list/tuple into a space separated string in python using a single line?
I tried doing: but it says str object is not callable. Is this doable using a single line? Answer Use string join() method. List: Tuple: Non-string objects:
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