How to get the count of Custom tuples against two lists

前端 未结 2 1724
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 09:01

Please help me to get the counter for the list SS2 in list SS1 in PYTHON using from collections import Counter or any other fastest way

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


        
2条回答
  •  不知归路
    2021-01-29 09:48

    credit to @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)
    

提交回复
热议问题