Home > Blockchain >  Finding ocurrences between two list py
Finding ocurrences between two list py

Time:06-24

I´m trying to comparate 2 list in py, then get the values repeat, put it in other list, and then get the index of the value in the very first original list, I got this

cecosXLS = ["0101", "0000", "0111", "0105"]
cecoManizales = ["0000", "0101", "0111", "0501", "0502"]
valoresCausa = ["795000","850000","140000","1450000"]
for x in range(len(cecoManizales)):
    comunesManizales = []
    temporaList = cecosXLS.copy()
    temporaList.sort()
    cecoManizales.sort()
    for item1, item2 in zip(temporaList, cecoManizales):
        if item1 == item2:
            comunesManizales.append(item1)
for x in range(len(comunesManizales)):
        print(comunesManizales[x])
for x in range(len(comunesManizales)):
    comun = comunesManizales[x]
    indiceComun = cecosXLS.index(comun)
    print(indiceComun)

As you can see, I sort the original list, in a new one, to comparate with the list i need to, then assign the values of the ocurrences in a new list, the problem here is this:

Very original list =  ['0101', '0000', '0111', '0105']
Sort of the original list =  ['0000', '0101', '0105', '0111']
List to comparate with the original sort =  ['0000', '0101', '0111', '0501', '0502']
List of ocurrences in both list =  ['0000', '0101']

But the total ocurrences are 3 instead of 2, '0000', '0101', '0111' but i can reach the last one, because is not in the same index in both of the sort list, how can i do this?

CodePudding user response:

Try replacing

    for item1, item2 in zip(temporaList, cecoManizales):
        if item1 == item2:
            comunesManizales.append(item1)

With:

    index = 0
    for item1 in temporaList:
        item2 = cecoManizales[index]
        while item1 > item2:
            index =1
        if item1 == item2:
            comunesManizales.append(item1)
            index =1

For long lists though, it may be a bit faster to count the objects in each list using a dict instead of sorting them.

CodePudding user response:

I see basically you want to compare the content of two list for elements which are present in both list. There is a pythonic way to do this instead of you going through all elements in a list.

def common_member(a, b):   
    a_set = set(a)
    b_set = set(b)
     
    # check length
    if len(a_set.intersection(b_set)) > 0:
        return(a_set.intersection(b_set)) 
    else:
        return("no common elements")

cecosXLS = ["0101", "0000", "0111", "0105"]
cecoManizales = ["0000", "0101", "0111", "0501", "0502"]
common_member(cecosXLS, cecoManizales)

The above code should give you all common elements between the two lists

CodePudding user response:

I think your issue is that you are looping with zip, which combines the elements from your list like (List1[0], List2[0]), (List1[1], List2[1]), (List1[2], List2[2]). Meaning if List1[0] == List2[1] you will never find it.

Instead, make two for loops, one nested in the other, to make sure you catch all combinations

for item1 in temporaList:
    for item2 in cecoManizales:
        if (item1 == item2) and (not (item1 in comunesManizales)):
            #only add if not added before
            comunesManizales.append(item1)
  • Related