简体   繁体   中英

Python counting number of items in list and storing in a dictionary

I have following list:

files_list = ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'movie1.mov', 'movie2.mov', 'doc1.pdf', 'doc2.pdf', 'doc3.pdf', 'doc4.pdf']

I want to count the number of items with a particular file extension and store it in a dictionary.

Expected output is:

extn_dict = {'jpg': 3, 'mov': 2, 'pdf': 4}

I'm writing following code:

for item in files_list:
    extn_dict[item[-3:]] = count(item) # I understand I should not have count() here but I'm not sure how to count them.

How can I count the number of items in the list with a particular extension?

>>> from collections import Counter
>>> files_list
['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'movie1.mov', 'movie2.mov', 'doc1.pdf', 'doc2.pdf', 'doc3.pdf', 'doc4.pdf']
>>> c = Counter(x.split(".")[-1] for x in files_list)
>>> c
Counter({'pdf': 4, 'jpg': 3, 'mov': 2})
>>> 

The easiest way is probably:

>>> d = {}
>>> for item in files_list:
...     d[item[-3:]] = d.get(item[-3:], 0) + 1
... 
>>> d
{'pdf': 4, 'mov': 2, 'jpg': 3}

The easiest way is to loop over the list and use a dictionary to store your counts.

files_list = ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'movie1.mov', 
              'movie2.mov', 'doc1.pdf', 'doc2.pdf', 'doc3.pdf', 'doc4.pdf']
counts = {}
for f in f:
    ext = f[-3:]
    if ext not in counts:
        counts[ext] = 0
    counts[ext] += 1

print counts
#{'pdf': 4, 'mov': 2, 'jpg': 3}

No doubt, there are other fancy solutions, but I think this is easier to understand.

If you can't assume that extension will always be 3 characters, then you can change the ext = line to:

ext = f.split(".")[-1]

As other posters have shown in their answers.

files_list = ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'movie1.mov', 'movie2.mov', 'doc1.pdf', 'doc2.pdf', 'doc3.pdf', 'doc4.pdf']
extension_set = [i.split('.')[-1] for i in files_list]
d = {j:extension_set.count(j) for j in extension_set}
print(d)

Analysis:

Current method - 10000 loops, best of 3: 25.3 µs per loop

Counter - 10000 loops, best of 3: 30.5 µs per loop(best of 3: 33.3 µs per loop with import statement)

itertools - 10000 loops, best of 3: 41.1 µs per loop(best of 3: 44 µs per loop with import statement)

You can use itertools.groupby :

import itertools
files_list = ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'movie1.mov', 'movie2.mov', 'doc1.pdf', 'doc2.pdf', 'doc3.pdf', 'doc4.pdf']
final_counts = {a:len(list(b)) for a, b in itertools.groupby(sorted(files_list, key=lambda x:x.split('.')[-1]), key=lambda x:x.split('.')[-1])}

Output:

{'pdf': 4, 'mov': 2, 'jpg': 3}

you can use the Counter function from collection module

from collections import Counter
files_list = ['pic1.jpg', 'pic2.jpg', 'pic3.jpg', 'movie1.mov', 'movie2.mov', 'doc1.pdf', 'doc2.pdf', 'doc3.pdf', 'doc4.pdf']
temp = []
for item in files_list:
    temp.append(item[-3:])

print Counter(temp)
>>> Counter({'pdf': 4, 'jpg': 3, 'mov': 2})

使用计数器和映射而不是列表理解

Counter(map(lambda x : x.split('.')[-1], files_list))

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