Home > database >  Compare elements within a list
Compare elements within a list

Time:03-09

List = [('j100', 'Jacob'), ('k150', 'Kevin'), ('d120', 'Davis'), ('d120', 'Davies'), ('c642', 'Charles'), ('s315', 'Stephen')]

I have this list of tuples and I want to make a nested loop to compare the first elements of the tuples together, to see if they are the same. so I want to compare j100 with k150 to see if they are the same etc. for all the elements.

Then I want to create a line of output if they are the same so d120=d120 I want the output to be "Davies and Davis have similar sounding names." and Davies and davis have to be in alphabetical order.

how would I construct something like this?

CodePudding user response:

you can use a list comprehension to do this concisely:

lst = [('j100', 'Jacob'), ('k150', 'Kevin'), ('d120', 'Davis'), ('d120', 'Davies'), ('c642', 'Charles'), ('s315', 'Stephen')]

[print(f'{i[1]} and {l[1]} have similar sounding names') for i in lst for j in lst if i[1] != j[1] and i[0] == j[0]]

Output:

Davis and Davies have similar sounding names
Davies and Davis have similar sounding names

This does it for every combination. You could break this down to a more readable loop and account for the doubles if you do not want them like so:

lst = [('j100', 'Jacob'), ('k150', 'Kevin'), ('d120', 'Davis'), ('d120', 'Davies'), ('c642', 'Charles'), ('s315', 'Stephen')]

seen = []
for i in lst:
    for j in lst:
        if i[1] != j[1] and i[0] == j[0]:
            setij = {i[1], j[1]}
            if setij not in seen:
                seen.append(setij)
                print(f'{i[1]} and {j[1]} have similar sounding names')

Output:

Davis and Davies have similar sounding names

CodePudding user response:

You can use a dict to group together names with the same tag:

List = [('j100', 'Jacob'), ('k150', 'Kevin'), ('d120', 'Davis'), ('d120', 'Davies'), ('c642', 'Charles'), ('s315', 'Stephen')]

sim = {}
for item in List:
    key,name = item
    if key in sim:
        sim[key].append(name)
    else:
        sim[key]=[name]


for _,names in sim.items():
    if len(names) > 1:
        names.sort()
        print(f'{" and ".join(names)} have similar sounding names.')

Output

Davies and Davis have similar sounding names.

CodePudding user response:

Slightly modified input:

List = [('d120', 'Davi'), ('j100', 'Jacob'), ('k150', 'Kevin'), ('d120', 'Davis'), ('d120', 'Davies'), ('c642', 'Charles'), ('s315', 'Stephen')]

You could use groupby:

from itertools import groupby, combinations
from operator import itemgetter

for _, group in groupby(sorted(List), key=itemgetter(0)):
    group = [name for _, name in group]
    if len(group) > 1:
        print(" and ".join(group)   " have similar sounding names.")

Output:

Davi and Davies and Davis have similar sounding names.

If you want pairwise ouput then you could try this with combinations:

for _, group in groupby(sorted(List), key=itemgetter(0)):
    group = [name for _, name in group]
    if len(group) > 1:
        for name1, name2 in combinations(group, 2):
            print(f"{name1} and {name2} have similar sounding names.")

Output:

Davi and Davies have similar sounding names.
Davi and Davis have similar sounding names.
Davies and Davis have similar sounding names.

CodePudding user response:

You can try something like this

List = [('j100', 'Jacob'), ('k150', 'Kevin'), ('d120', 'Davis'), ('d120', 'Davies'), ('c642', 'Charles'), ('s315', 'Stephen')]

for a in range(len(List)-1):
    a_item = List[a]
    for b in range(a 1,len(List)):
        b_item = List[b]
        
        if a_item[0] == b_item[0]:
            result = [a_item[1],b_item[1]]
            result.sort()
            print(f'{result[0]} and {result[1]} have similar sounding names.')
            
  • Related