简体   繁体   中英

Better way to iterate through list and store output in a new list

I am a beginner in python and I would like your opinion on a more sophisticated way to write the code that follows. I want the code to iterate through the whole list (x) of items, count how many times each item appears and store the output in a new list (z). Here is the code (it works as it is) :

x = ["apple", "orange", "cherry", "apple"]

def new_list(a):
    z=[]
    for i in x:
        y = x.count(i)
        (z.append(y))
    return z

print(new_list(x)) 

I would suggest to do a list comprehension:

>>> x = ["apple", "orange", "cherry", "apple"]
>>> [x.count(val) for val in x]
[2, 1, 1, 2]
>>> 

Or even better with map :

>>> [*map(x.count, x)]
[2, 1, 1, 2]
>>> 

With using list.count() order of your programming is O(n^2) and with Counter and hashmap order of your programmer is O(n) . Try this:

x = ["apple", "orange", "cherry", "apple"]

[x.count(i) for i in x]
# [2,1,1,2]

Or use Counter :

from collections import Counter
dct = Counter(x)
# Counter({'apple': 2, 'orange': 1, 'cherry': 1})

[dct[i] for i in x]
# [2, 1, 1, 2]

Check Runtime: ( Counter is faster than list.count() )

在此处输入图片说明

Try using the Counter class:

from collections import Counter
x = ["foo", "bar", "foo", "foo"];
z = Counter(x)
print(z)
# Output: {"foo": 3, "bar": 1} 

You can use the Counter class in collections. So iterate over input list and then find count of every element.

from collections import Counter
x = ["apple", "orange", "cherry", "apple"]
c = Counter(x)
print([c[item] for item in x])
#[2, 1, 1, 2]

You can use the Counter subclass of the collections module:

from collections import Counter

x = ["apple", "orange", "cherry", "apple"]


counter = Counter(x)
z = list(counter.values())

print(z) #[2,1,1]

I didn't use count because it may not be good for performance which is also pointed out by this answer .

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