简体   繁体   中英

Python : Counting unique values in list

I'm trying to add up the number of equal values in a list. The list looks like this:

list = [["APP", "X", "v3", "CN_L", "2"],
           ["APP2", "X", "v3", "CN_M", "2"],
           ["APP3", "Z", "v3", "CN_L", "2"],
           ["APP2", "Z", "v3", "CN_M", "2"]]

etc.

I am mainly concerned with the number of times the 4th field is found.

I am not very experienced in Python. I had already found something about Counter and I tried something with it.

from collections import Counter
list = [["APP", "X", "v3", "CN_L", "2"],
       ["APP2", "X", "v3", "CN_M", "2"],
       ["APP3", "Z", "v3", "CN_L", "2"],
       ["APP2", "Z", "v3", "CN_M", "2"]]

distinct_list=(Counter(list).keys())

Without for loop I get nothing from this code, and get an unhashable type back. Who can push me in the right direction?

Use [l[3] for l in my_list] to get the elements at index 3 (4th elements), then simply calling Counter on your list will give you the unique elements and their count.

from collections import Counter

my_list = [["APP", "X", "v3", "CN_L", "2"],
           ["APP2", "X", "v3", "CN_M", "2"],
           ["APP3", "Z", "v3", "CN_L", "2"],
           ["APP2", "Z", "v3", "CN_M", "2"]]

forth_elts = [l[3] for l in my_list]

print(Counter(forth_elts))


>>> Counter({'CN_M': 2, 'CN_L': 2})

And please avoid using keywords and other words such as "str", or "list" to name your variables.

from collections import Counter
    
new_list = [["APP", "X", "v3", "CN_L", "2"],
               ["APP2", "X", "v3", "CN_M", "2"],
               ["APP3", "Z", "v3", "CN_L", "2"],
               ["APP2", "Z", "v3", "CN_M", "2"]]

#import numpy library
import numpy as np

#convert the list into a numpy array 
arr=np.array(new_list)

#take the 4 th column and then apply the counter 
result=Counter(arr[:,4])

I would put the data into a pandas Dataframe like this:

import pandas as pd

df = pd.DataFrame(
[["APP", "X", "v3", "CN_L", "2"],
["APP2", "X", "v3", "CN_M", "2"],
["APP3", "Z", "v3", "CN_L", "2"],
["APP2", "Z", "v3", "CN_M", "2"]]
)

df[4].value_counts()

->

2    4
Name: 4, dtype: int64

It will return you a pandas Series which is basically working like a dict so you can do:

x = df[4].value_counts()
x["2"] --> 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM