Home > Back-end >  in Python, I can't seem to find the same items in a list
in Python, I can't seem to find the same items in a list

Time:12-25

tuple= [['nike airjordan kahverengi', 42, 6],['nike airjordan sarı', 42, 10], ['nike airjordan mavi', 42, 8],['nike airjordan yeşil', 42, 9]['nike airjordan sarı', 42, 10]]

def getOrderList(self):
        orderSplit = ''.join(self.orderText).splitlines()
        productList = []
        for idx, i in enumerate(orderSplit, start = 0):
            if(idx > self.starting_point):
                if i == '@':
                    break
                else:    
                    productList.append([i.rstrip(i[-3:]), int(i[-2:]),idx])
            

        return productList

this is the function which gets tuple

orderList = self.product.getOrderList()

here I am looking for the same ones in the tuple from now on

CodePudding user response:

You can map your lists to tuples and use collections.Counter:

lsts = [['nike airjordan kahverengi', 42, 6],['nike airjordan sarı', 42, 10],
        ['nike airjordan mavi', 42, 8],['nike airjordan yeşil', 42, 9],
        ['nike airjordan sarı', 42, 10]]

from collections import Counter
counts = Counter(map(tuple, lsts))

for k,v in counts.items():
    print(list(k), 'appears', v, 'time(s).')

Output:

['nike airjordan kahverengi', 42, 6] appears 1 time(s).
['nike airjordan sarı', 42, 10] appears 2 time(s).
['nike airjordan mavi', 42, 8] appears 1 time(s).
['nike airjordan yeşil', 42, 9] appears 1 time(s).

  • Related