count item in list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: count item in list (/thread-34141.html) |
count item in list - korenron - Jun-30-2021 Hello, how can I count part of items in list? to be clear : I have a list that build in this form Final_List= [['10.0.0.1:Success', '10.0.0.2:Success', '10.0.0.3:Error', '10.0.0.4:Offline' , '10.0.0.5:Error']] I want to know how much Succes\error\offline there is in the list (i don't mind the IP - just the total number) I have try to run ok = 0 error = 0 offline = 0 for item in IP_List: if 'Success' in item: ok +=1 if 'Error' in item: error +=1 if 'offline' in item: offline += 1maybe I used list.appand wrong? Final_List.append(p.map(GetUplaudFies, Device_List))GetUplaudFies return this return ip + ":" + Upload_Status RE: count item in list - deanhystad - Jun-30-2021 Use a counting dictionary. import collections items = ['A', 'B', 'A', 'B', 'C', 'A', 'A'] counter = collections.Counter(items) for item, count in counter.items(): print(item, count)
RE: count item in list - korenron - Jun-30-2021 I saw this but will it work ? beacuse every item in the list is number:state and I'm only want to count the state . for my list will it not give me 200 results? I have 200 differents IP inside RE: count item in list - deanhystad - Jun-30-2021 p.map is giving you a list, so there is no reason to append unless you want Final_List to be a list of lists. Your code fails because FinalList[0] is ['10.0.0.1:Success', '10.0.0.2:Success', '10.0.0.3:Error', '10.0.0.4:Offline' , '10.0.0.5:Error'], not '10.0.0.1:Success' as you are expecting. Change the code to something like this: Final_List = p.map(GetUplaudFies, Device_List)You can use a counter dictionary. But maybe it isn't right for this application since there are only 3 possible outcomes and they are all known. Your code should work with the above change. But this is a way to do it with a Counter outcomes = [outcome.split(':')[1] for outcome in Final_List] outcomes = collections.Counter(outcomes) for item, count in counter.items(): print(item, count) RE: count item in list - korenron - Jun-30-2021 i guees this was my mistake the appand after the map I didn't realize it create my a list well - everyday you learn somehting :-) thank you ! RE: count item in list - DeaD_EyE - Jun-30-2021 The Counter from collections is helpful. from collections import Counter def my_filter(iterable): """ Generator returns the state: 'Success', 'Error', 'Offline' """ for element in iterable: try: ip, kind = element.split(":", maxsplit=1) except ValueError: continue yield kind def get_results(chunks): """ Yield all chunks as Counter object """ for chunk in chunks: yield Counter(my_filter(chunk)) def sum_results(chunks): """ Sum all chunks together and return one Counter object """ final = Counter() for result in get_results(chunks): final += result return final input_data = [ [ '10.0.0.1:Success', '10.0.0.2:Success', '10.0.0.3:Error', '10.0.0.4:Offline', '10.0.0.5:Error', ] ] results1 = sum_results(input_data) results2 = sum_results(input_data * 10) print(results1) print(results2) If input_data is a flat list, get_results(input_data) does not require the for-loop and just return one Counter object.FlatList-Example: from collections import Counter def my_filter(iterable): """ Generator returns the state: 'Success', 'Error', 'Offline' """ for element in iterable: try: ip, kind = element.split(":", maxsplit=1) except ValueError: continue yield kind input_data = [ '10.0.0.1:Success', '10.0.0.2:Success', '10.0.0.3:Error', '10.0.0.4:Offline', '10.0.0.5:Error', ] result1 = Counter(my_filter(input_data)) result2 = Counter(my_filter(input_data * 10)) print(result1) print(result2) You should also read: https://docs.python.org/3/library/collections.html#collections.Counter
RE: count item in list - korenron - Jul-01-2021 thanks I will give it also a try RE: count item in list - deanhystad - Jul-01-2021 What do you think should happen when you append a list to a list? Did you think it would make the original list longer? Append (and insert) treat the argument as an object, not an iterable. From the docs: Quote:list.append(x)You could use extend to add items from a list to an existing list. And as the extend description from the manual hints, you can do the same thing with slices. You can also concatenate lists using '+'Lists are important in Python. You need to REALLY understand how they work or it is going to come back to bite you again and again. RE: count item in list - naughtyCat - Aug-18-2021 items = ['A', 'B', 'A', 'B', 'C', 'A', 'A'] key = sorted(set(items)) for k in key: print(k, items.count(k))
|