Posts: 257
Threads: 108
Joined: May 2019
Jun-30-2021, 01:23 PM
(This post was last modified: Jun-30-2021, 01:23 PM by korenron.)
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
1 2 3 4 5 6 7 8 9 10 |
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 + = 1
|
maybe I used list.appand wrong?
1 |
Final_List.append(p. map (GetUplaudFies, Device_List))
|
GetUplaudFies return this
1 |
return ip + ":" + Upload_Status
|
Posts: 6,783
Threads: 20
Joined: Feb 2020
Use a counting dictionary.
1 2 3 4 5 6 |
import collections
items = [ 'A' , 'B' , 'A' , 'B' , 'C' , 'A' , 'A' ]
counter = collections.Counter(items)
for item, count in counter.items():
print (item, count)
|
Output: A 4
B 2
C 1
Posts: 257
Threads: 108
Joined: May 2019
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
Posts: 6,783
Threads: 20
Joined: Feb 2020
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:
1 |
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
1 2 3 4 |
outcomes = [outcome.split( ':' )[ 1 ] for outcome in Final_List]
outcomes = collections.Counter(outcomes)
for item, count in counter.items():
print (item, count)
|
Posts: 257
Threads: 108
Joined: May 2019
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 !
Posts: 2,125
Threads: 11
Joined: May 2017
The Counter from collections is helpful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
from collections import Counter
def my_filter(iterable):
for element in iterable:
try :
ip, kind = element.split( ":" , maxsplit = 1 )
except ValueError:
continue
yield kind
def get_results(chunks):
for chunk in chunks:
yield Counter(my_filter(chunk))
def sum_results(chunks):
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)
|
Output: Counter({'Success': 2, 'Error': 2, 'Offline': 1})
Counter({'Success': 20, 'Error': 20, 'Offline': 10})
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from collections import Counter
def my_filter(iterable):
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)
|
Output: Counter({'Success': 2, 'Error': 2, 'Offline': 1})
Counter({'Success': 20, 'Error': 20, 'Offline': 10})
You should also read: https://docs.python.org/3/library/collec...ns.Counter
tester_V and Pedroski55 like this post
Posts: 257
Threads: 108
Joined: May 2019
thanks
I will give it also a try
Posts: 6,783
Threads: 20
Joined: Feb 2020
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)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
You could use extend to add items from a list to an existing list.
Output: list.extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable
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.
Posts: 45
Threads: 0
Joined: Aug 2021
1 2 3 4 |
items = [ 'A' , 'B' , 'A' , 'B' , 'C' , 'A' , 'A' ]
key = sorted ( set (items))
for k in key:
print (k, items.count(k))
|
Output: A 4
B 2
C 1
|