检查字典中是否有多个整数值,其中字典值是整数列表

时间:2018-08-02 12:33:34

标签: python list loops dictionary

我有这样的字典。

{1: [1], 2: [1, 2], 3: [1, 2, 3], 19: [1, 2, 19], 4: [1, 2, 3, 4], 23: [1, 2, 3, 23], 20: [1, 2, 19, 20], 5: [1, 2, 3, 4, 5], 24: [1, 2, 3, 23, 24], 21: [1, 2, 19, 20, 21], 6: [1, 2, 3, 4, 5, 6], 25: [1, 2, 3, 23, 24, 25], 22: [1, 2, 19, 20, 21, 22], 7: [1, 2, 3, 4, 5, 6, 7], 26: [1, 2, 3, 4, 5, 6, 26], 8: [1, 2, 3, 4, 5, 6, 7, 8], 27: [1, 2, 3, 4, 5, 6, 26, 27], 9: [1, 2, 3, 4, 5, 6, 7, 8, 9], 28: [1, 2, 3, 4, 5, 6, 26, 27, 28], 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 29: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29], 11: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 30: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30], 12: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 31: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30, 31], 13: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 32: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30, 31, 32], 14: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 33: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30, 31, 32, 33], 15: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 16: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 17: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 18: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]}

我想知道[2,3]存在多少个值。同样,我想对其他[3,4][4,5]等货币对这样做。

6 个答案:

答案 0 :(得分:0)

您可以将sumall和生成器表达式一起使用。给定输入字典d

L = [2, 3]
res = sum(all(i in v for i in L) for v in d.values())  # 27

请注意,这会检查每个列表中 all 指定值是否存在某处,即与顺序无关。

为性能而哈希

要获得更高的效率,您应该set输入字典值,例如通过一次转换:

d = {k: set(v) for k, v in d.items()}

执行此转换后,可以使用set.issubset来提高O(1)查找的复杂度:

L = [2, 3]
L_set = {2, 3}
res = sum(L_set.issubset(v) for v in d.values())  # 27

答案 1 :(得分:0)

从空闲状态开始:

>>> if all( x in list(range(5)) for x in [2, 3]):
    print (1)   
1

list(range(5))模拟列表[0, 1, 2, 3, 4]

这样,您需要遍历字典中的每个键,可以这样完成:

for key in myDict:
    #Do something

答案 2 :(得分:0)

这将检查dict中的key值是否为{2,3}的超集:

for k,v in dictionary.items():
    if set(v) >= {2,3}:
        print(v)

答案 3 :(得分:0)

要查看[2,3]有多少次:

sum([1 for x in d.values() if set([2, 3]).issubset(x)])

其中d是您的字典。

您还可以获取其值包含[2,3](并使用其中的len)的键的列表:

[y for y,x in d.items() if set([2, 3]).issubset(x)]

答案 4 :(得分:0)

使用itertools.combinations创建对,并找到包含这些对的所有列表中的sum

>>> d = {1: [1], 2: [1, 2], 3: [1, 2, 3], 19: [1, 2, 19], 4: [1, 2, 3, 4], 23: [1, 2, 3, 23], 20: [1, 2, 19, 20], 5: [1, 2, 3, 4, 5], 24: [1, 2, 3, 23, 24], 21: [1, 2, 19, 20, 21], 6: [1, 2, 3, 4, 5, 6], 25: [1, 2, 3, 23, 24, 25], 22: [1, 2, 19, 20, 21, 22], 7: [1, 2, 3, 4, 5, 6, 7], 26: [1, 2, 3, 4, 5, 6, 26], 8: [1, 2, 3, 4, 5, 6, 7, 8], 27: [1, 2, 3, 4, 5, 6, 26, 27], 9: [1, 2, 3, 4, 5, 6, 7, 8, 9], 28: [1, 2, 3, 4, 5, 6, 26, 27, 28], 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 29: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29], 11: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 30: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30], 12: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 31: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30, 31], 13: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 32: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30, 31, 32], 14: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 33: [1, 2, 3, 4, 5, 6, 26, 27, 28, 29, 30, 31, 32, 33], 15: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 16: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 17: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], 18: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]}
>>>
>>> from itertools import combinations
>>> elems = set(e for l in d.values() for e in l)
>>> pairs = combinations(elems, 2)
>>> res = {pair:sum(1 for l in d.values() if all(e in l for e in pair)) for pair in pairs}
>>> {k:v for k,v in res.items() if v}
{(1, 2): 32, (1, 3): 27, (1, 4): 23, (1, 5): 22, (1, 6): 21, (1, 7): 12, (1, 8): 11, (1, 9): 10, (1, 10): 9, (1, 11): 8, (1, 12): 7, (1, 13): 6, (1, 14): 5, (1, 15): 4, (1, 16): 3, (1, 17): 2, (1, 18): 1, (1, 19): 4, (1, 20): 3, (1, 21): 2, (1, 22): 1, (1, 23): 3, (1, 24): 2, (1, 25): 1, (1, 26): 8, (1, 27): 7, (1, 28): 6, (1, 29): 5, (1, 30): 4, (1, 31): 3, (1, 32): 2, (1, 33): 1, (2, 3): 27, (2, 4): 23, (2, 5): 22, (2, 6): 21, (2, 7): 12, (2, 8): 11, (2, 9): 10, (2, 10): 9, (2, 11): 8, (2, 12): 7, (2, 13): 6, (2, 14): 5, (2, 15): 4, (2, 16): 3, (2, 17): 2, (2, 18): 1, (2, 19): 4, (2, 20): 3, (2, 21): 2, (2, 22): 1, (2, 23): 3, (2, 24): 2, (2, 25): 1, (2, 26): 8, (2, 27): 7, (2, 28): 6, (2, 29): 5, (2, 30): 4, (2, 31): 3, (2, 32): 2, (2, 33): 1, (3, 4): 23, (3, 5): 22, (3, 6): 21, (3, 7): 12, (3, 8): 11, (3, 9): 10, (3, 10): 9, (3, 11): 8, (3, 12): 7, (3, 13): 6, (3, 14): 5, (3, 15): 4, (3, 16): 3, (3, 17): 2, (3, 18): 1, (3, 23): 3, (3, 24): 2, (3, 25): 1, (3, 26): 8, (3, 27): 7, (3, 28): 6, (3, 29): 5, (3, 30): 4, (3, 31): 3, (3, 32): 2, (3, 33): 1, (4, 5): 22, (4, 6): 21, (4, 7): 12, (4, 8): 11, (4, 9): 10, (4, 10): 9, (4, 11): 8, (4, 12): 7, (4, 13): 6, (4, 14): 5, (4, 15): 4, (4, 16): 3, (4, 17): 2, (4, 18): 1, (4, 26): 8, (4, 27): 7, (4, 28): 6, (4, 29): 5, (4, 30): 4, (4, 31): 3, (4, 32): 2, (4, 33): 1, (5, 6): 21, (5, 7): 12, (5, 8): 11, (5, 9): 10, (5, 10): 9, (5, 11): 8, (5, 12): 7, (5, 13): 6, (5, 14): 5, (5, 15): 4, (5, 16): 3, (5, 17): 2, (5, 18): 1, (5, 26): 8, (5, 27): 7, (5, 28): 6, (5, 29): 5, (5, 30): 4, (5, 31): 3, (5, 32): 2, (5, 33): 1, (6, 7): 12, (6, 8): 11, (6, 9): 10, (6, 10): 9, (6, 11): 8, (6, 12): 7, (6, 13): 6, (6, 14): 5, (6, 15): 4, (6, 16): 3, (6, 17): 2, (6, 18): 1, (6, 26): 8, (6, 27): 7, (6, 28): 6, (6, 29): 5, (6, 30): 4, (6, 31): 3, (6, 32): 2, (6, 33): 1, (7, 8): 11, (7, 9): 10, (7, 10): 9, (7, 11): 8, (7, 12): 7, (7, 13): 6, (7, 14): 5, (7, 15): 4, (7, 16): 3, (7, 17): 2, (7, 18): 1, (8, 9): 10, (8, 10): 9, (8, 11): 8, (8, 12): 7, (8, 13): 6, (8, 14): 5, (8, 15): 4, (8, 16): 3, (8, 17): 2, (8, 18): 1, (9, 10): 9, (9, 11): 8, (9, 12): 7, (9, 13): 6, (9, 14): 5, (9, 15): 4, (9, 16): 3, (9, 17): 2, (9, 18): 1, (10, 11): 8, (10, 12): 7, (10, 13): 6, (10, 14): 5, (10, 15): 4, (10, 16): 3, (10, 17): 2, (10, 18): 1, (11, 12): 7, (11, 13): 6, (11, 14): 5, (11, 15): 4, (11, 16): 3, (11, 17): 2, (11, 18): 1, (12, 13): 6, (12, 14): 5, (12, 15): 4, (12, 16): 3, (12, 17): 2, (12, 18): 1, (13, 14): 5, (13, 15): 4, (13, 16): 3, (13, 17): 2, (13, 18): 1, (14, 15): 4, (14, 16): 3, (14, 17): 2, (14, 18): 1, (15, 16): 3, (15, 17): 2, (15, 18): 1, (16, 17): 2, (16, 18): 1, (17, 18): 1, (19, 20): 3, (19, 21): 2, (19, 22): 1, (20, 21): 2, (20, 22): 1, (21, 22): 1, (23, 24): 2, (23, 25): 1, (24, 25): 1, (26, 27): 7, (26, 28): 6, (26, 29): 5, (26, 30): 4, (26, 31): 3, (26, 32): 2, (26, 33): 1, (27, 28): 6, (27, 29): 5, (27, 30): 4, (27, 31): 3, (27, 32): 2, (27, 33): 1, (28, 29): 5, (28, 30): 4, (28, 31): 3, (28, 32): 2, (28, 33): 1, (29, 30): 4, (29, 31): 3, (29, 32): 2, (29, 33): 1, (30, 31): 3, (30, 32): 2, (30, 33): 1, (31, 32): 2, (31, 33): 1, (32, 33): 1}

答案 5 :(得分:0)

简短回答

使用集合和列表理解来提高效率。

sample_dict= {1: [1, 2, 3, 4, 5], 2: [6, 3, 7, 4, 1, 2]}
search_set = {2, 3}

count = len([i for i in sample_dict.values() if set(i) >= search_set])

这将生成与超集条件匹配的项目的新列表。该新列表的len是匹配项的数量。

逐步说明

1。)遍历字典的值:

i for i in sample_dict.values()

2。)检查字典值列表中的每一项(它们本身都是列表,转换为快速集相交操作的集合)是否是搜索集的超集。

if set(i) >= search_set

注意:集合中项目的顺序无关紧要。

3。)仅保留符合步骤2中条件的项目(原始字典值列表)。

4。)获取列表中其余项目的长度:len()