【问题标题】:How to get the count of Custom tuples against two lists如何根据两个列表获取自定义元组的计数
【发布时间】:2017-11-13 11:28:26
【问题描述】:

请帮助我使用 from collections import Counter 或任何其他最快的方式获取 PYTHON 中列表 SS1 中列表 SS2 的计数器

SS1 = [(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)]

SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5),
(1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6),
(3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)]

这是我尝试过的,但我不知道如何获取每个元组的第 (1,2,4) 个元素的计数

SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)]

from collections import Counter
c = Counter(elem[0:3] for elem in SS1[0:6])

for k, v in c.items():
    if (v > 0):
        print(k,v)

现在这对于 0:3 来说是完美的,但我想要得到的不是 1,2,3 的计数,而是我想要 1,2,4 每个元组的元素计数。

抱歉各位,希望你们能理解我的问题……再次抱歉,我是这条蟒蛇的新手。

【问题讨论】:

  • 您能否通过添加此输入的预期输出来澄清您的问题?
  • 你想完成什么?
  • from collections import Counter c = Counter(elem[0:3] for elem in SS1[0:6]) for k, v in c.items(): if (v > 0): print(k,v) 这仅给出以下值 (1, 2, 3) 3 (1, 2, 4) 1 (1, 3, 4) 1 (2, 3, 4) 1 但我想知道对于我的第二组 SS2 中的所有值
  • 请在问题中添加预期输出,而不是 cmets。另外,自定义元组是什么意思?
  • 或者让我知道如何在 elem[0:3] 中为 silice(2,3,5) 指定

标签: python tuples counter slice iterable


【解决方案1】:

感谢@Chiheb Nexus

SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)]

from collections import Counter

def get_new_list(a, pos):
    # Check if any element in pos is > than the length of the tuples
    if any(k >= len(min(SS1, key=lambda x: len(x))) for k in pos):
        return

    for k in a:
        yield tuple(k[j] for j in pos)

def elm_counter(elm):
    if not len(elm):
        return 

    c = Counter(elm)
    for k, v in c.items():
        if v > 0:
            print(k, v)
elm = list(get_new_list(SS1, (2,)))
elm_counter(elm)
print('---')    
elm = list(get_new_list(SS1, (0, 2, 3)))
elm_counter(elm)
print('---')
elm = list(get_new_list(SS1, (1, 3, 4)))
elm_counter(elm)

【讨论】:

  • 我不知道这是否可以作为答案接受。我认为您应该删除此问题,因为它与此 answer 重复
【解决方案2】:

好吧,我假设你想要什么作为你的输出,因为你不清楚。所以基本上你想要的是在 SS2 中找到 SS1 中 items 的计数。

例如(1,4,5)在SS1中出现的次数

这将是 3 即(1, 2, 3, 4, 5),(1, 2, 4, 5, 6),(1, 3, 4, 5, 6)

所以(1, 2, 5) 又是 3 对吧?存在于 (1, 2, 3, 4, 5),(1, 2, 3, 5, 6),(1, 2, 4, 5, 6)

我想你可能需要的是。

set(tuple2).issubset(tuple1)

所以这里是你的问题的代码:

SS1 = [(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)]
SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5),
(1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6),
(3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)]
count=0
count_list = []
for ss2item in SS2:
    for ss1item in SS1:
        if set(ss2item).issubset(ss1item):
            count+=1
    count_list.append(count)        
    count=0
print(count_list)

它的输出将是 SS2 中每个项目的计数列表:

[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

【讨论】:

  • 感谢您的尝试,这并不是我想要的以及您建议的方式在 python 中可能不是最快的,因为我正在处理数百万条记录...所以计数器将是最好的选择...谢谢哥们
  • 好的,如果您明确指定输出,那么将给出一个解决方案。但是输出是正确的吧?
  • 你可以看到我在这里发布的答案,我无法选择我的答案,似乎需要等待两天
猜你喜欢
  • 2021-05-13
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 2013-09-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多