简体   繁体   English

计算python中的特定字典条目

[英]counting specific dictionary entries in python

I have a python dictionary and I want to count the amount of keys that have a specific format.我有一个 python 字典,我想计算具有特定格式的键的数量。 The keys that I want to count are all the keys that have the format 'letter, number, number'.我要计算的键是所有具有“字母、数字、数字”格式的键。 In my specific case the key always begins with the letter 'A'.在我的特定情况下,键总是以字母“A”开头。 only the numbers change.只是数字发生了变化。 Example: A12, A16, A71示例:A12、A16、A71

For example I want to count all the entries that have this AXX format (where the X's are numbers).例如,我想计算所有具有此 AXX 格式的条目(其中 X 是数字)。

{'A34': 83, 'B32': 70, 'A44': 66, A12: 47, 'B90': 71}

I know I can count all the entries of my dictionary by using:我知道我可以使用以下方法计算字典中的所有条目:

print(len(my_dict.keys()))

but how do I count up all the entries that have the specific format I need.但是我如何计算所有具有我需要的特定格式的条目。

You can use a generator comprehension inside the sum function:您可以在sum函数中使用生成器sum

print(sum(1 for k in d.keys() if k.startswith('A') and len(k) == 3 and k[1:3].isdigit()))

This does three checks: if the key starts with A, if the length of this key is 3 and if the last two characters of this key is a digit.这会进行三项检查:密钥是否以 A 开头,该密钥的长度是否为 3,以及该密钥的最后两个字符是否为数字。

You can also use Regex:您还可以使用正则表达式:

import re
print(sum(1 for k in d.keys() if re.match('^A\\d{2}$', k)))

Both snippets outputs 3.两个片段都输出 3。

You can try list comprehension.您可以尝试列表理解。

len([key for key in list(my_dict.keys()) if 'A' in key])

For your specific condition, we can try the below, if you need to be more specific then write a regex in the if clause.对于您的具体情况,我们可以尝试以下方法,如果您需要更具体,请在 if 子句中编写正则表达式。

len([key for key in list(my_dict.keys()) if ((key.startswith('A')) and (len(key)==3))])

Should work!应该管用!

Go through all possibilities and check?检查所有可能性并检查?

result = sum(f'A{i:02}' in my_dict for i in range(100))

Benchmark along with the solutions from the accepted answer, with a dict like you described your real one ("about 5000 items" and "all my A values have 2 digits. However other values that I have such as B and C values will have 3 digits."):基准以及已接受答案中的解决方案,使用像您描述的真实答案一样的字典(“大约 5000 个项目”和“我所有的 A 值都有 2 位数。但是,我拥有的其他值,例如 B 和 C 值将有 3数字。”):

  41.5 μs  sum(f'A{i:02}' in my_dict for i in range(100)) 
 573.5 μs  sum(1 for k in my_dict.keys() if k.startswith('A') and len(k) == 3 and k[1:3].isdigit())
3546.0 μs  sum(1 for k in my_dict.keys() if re.match('^A\d{2}$', k))

Benchmark code ( Try it online! ):基准代码( 在线试用! ):

from timeit import repeat

setup = '''
from random import sample
from string import ascii_uppercase as letters
import re

A = [f'A{i:02}' for i in range(100)]
B2Z = [f'{letter}{i}' for letter in letters for i in range(10, 1000)]
A2Z = sample(A + sample(B2Z, 4900), 5000)
my_dict = dict.fromkeys(A2Z)
'''

E = [
    "sum(f'A{i:02}' in my_dict for i in range(100))",
    "sum(1 for k in my_dict.keys() if k.startswith('A') and len(k) == 3 and k[1:3].isdigit())",
    "sum(1 for k in my_dict.keys() if re.match('^A\\d{2}$', k))",
]

for _ in range(3):
    for e in E:
        number = 10
        t = min(repeat(e, setup, number=number)) / number
        print('%6.1f μs ' % (t * 1e6), e)
    print()
import re

my_dict = { ... }
filtered = filter(lambda k: bool(re.match("^[A-Z][0-9]{2}", k)), my_dict.keys())
print(len(filtered))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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