Home > Enterprise >  How to compare the number of occurrences in strings in 2 different lists?
How to compare the number of occurrences in strings in 2 different lists?

Time:02-25

I have a dictionary containing 2 lists A and B. Please refer to the example.

Lists A and B contain multiple strings where each string contains numbers which are comma-delimited.

My question is, how do I compare the number of occurrences of each string in list A with the string with the same index in string B and then print only the ones that are equal? e.g. The last string of A contains only 2 elements whereas the one in B contains 3, thus making it not equal whereas the other strings are all equal to each other in terms of the number of elements in each string.

data = {
    'A' : ["2,3,4,5,6,10", "3,4,5,6,7", "2,3,4", "4,5,6,7,8,9,1", "1,2"],
    'B' : ["2,2,2,2,2,3", "2,2,2,2,2", "1,2,1", "1,1,1,1,1,1,1", "3,4,5"]
}

what I currently have come up with:

new_list_1 = []
new_list_2 = []

a = data['A']
b = [[int(x) for x in i.split(',')] for i in a]


e = data['B']
f = [[int(x) for x in i.split(',')] for i in e]
# print(f)


for x,y in zip(b, f):
    if len(x) == len(y):
        new_list_1.append(b)
        new_list_2.append(f)

print(new_list_2)

However, this gives the following as output:

[[[2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2], [1, 2, 1], [1, 1, 1, 1, 1, 1, 1], [3, 4, 5]], [[2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2], [1, 2, 1], [1, 1, 1, 1, 1, 1, 1], [3, 4, 5]], [[2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2], [1, 2, 1], [1, 1, 
1, 1, 1, 1, 1], [3, 4, 5]], [[2, 2, 2, 2, 2, 3], [2, 2, 2, 2, 2], [1, 2, 1], [1, 1, 1, 1, 1, 1, 1], [3, 4, 5]]]

CodePudding user response:

I think this is what you're trying to do:

data = {
    'A' : ["2,3,4,5,6,10", "3,4,5,6,7", "2,3,4", "4,5,6,7,8,9,1", "1,2"],
    'B' : ["2,2,2,2,2,3", "2,2,2,2,2", "1,2,1", "1,1,1,1,1,1,1", "3,4,5"]
}
newlist = []
for x, y in zip(data['A'], data['B']):
    if len(x.split(',')) == len(y.split(',')):
        newlist.append([x, y])
print(newlist)

Output:

[['2,3,4,5,6,10', '2,2,2,2,2,3'], ['3,4,5,6,7', '2,2,2,2,2'], ['2,3,4', '1,2,1'], ['4,5,6,7,8,9,1', '1,1,1,1,1,1,1']]

CodePudding user response:

Given your data as:

data = {
    'A' : ["2,3,4,5,6,10", "3,4,5,6,7", "2,3,4", "4,5,6,7,8,9,1", "1,2"],
    'B' : ["2,2,2,2,2,3", "2,2,2,2,2", "1,2,1", "1,1,1,1,1,1,1", "3,4,5"]
}

You can get back a list of tuples where the lengths of the values cast from strings to lists match via zip() and a list comprehension like:

results = [
    (a, b) for (a, b) in zip(*data.values())
    if len(a.split(",")) == len(b.split(","))
]
print(results)

This will give you:

[
    ('2,3,4,5,6,10', '2,2,2,2,2,3'),
    ('3,4,5,6,7', '2,2,2,2,2'),
    ('2,3,4', '1,2,1'),
    ('4,5,6,7,8,9,1', '1,1,1,1,1,1,1')
]

A slightly simpler version that does not use values() that produces the same result would be:

results = [
    (a, b) for (a, b) in zip(data["A"], data["B"])
    if len(a.split(",")) == len(b.split(","))
]
print(results)
  • Related