Home > database >  Python list compare new names
Python list compare new names

Time:10-06

in my project I am having a long list of names where I scan the last 20. In this list I sometimes have a new name that pops up in between. I need to find this new name and the index of it, and currently I have a working script. The only problem is that it does not work if a new name pops up that is a duplicate with someone elses:

new = ['Bram', 'Vincent', 'Arthur', 'Perry', 'Bram', 'Sebastiaan', 'Arkadiusz', 'Felix', 'Suzanne', 'Maurice', 'Mohahmed', 'Lars', 'De Wet', 'Andre', 'Arjan', 'Frans', 'Andre', 'Guleed', 'Sebastian', 'Mark', 'Anne-marijke']
old = ['Bram', 'Vincent', 'Arthur', 'Perry', 'Bram', 'Sebastiaan', 'Arkadiusz', 'Felix', 'Suzanne', 'Maurice', 'Mohahmed', 'Lars', 'De Wet', 'Andre', 'Arjan', 'Frans', 'Guleed', 'Sebastian', 'Mark', 'Anne-marijke', 'Karel']

newname = list(set(new) - set(old))

In the new list there is a duplicate of 'Andre', could I find just that new 'Andre' in the list?

I tried the following, but this also does not work when the names are next to eachother:

newname = None
for x in range(20):

    if new[x] == old[x]:
        continue
    if new[x] == old[x - 1]:
        continue
    if new[x] == old[ 1]:
        continue
    else:
        newname = new[x]

print(newname)

CodePudding user response:

You could use counters to detect when a name has more occurences in new than it had in old.

from collections import Counter

c_old = dict(Counter(old))
c_new = dict(Counter(new))

new_names = [k for k,v in c_new.items() if k not in c_old or v > c_old[k]]

With your sample data it gives as expected:

['Andre']

CodePudding user response:

Actually, when I tried your code, I saw your code is working. Change old[ 1] to old[x 1] and see the output 'Andre'

new = ['Bram', 'Vincent', 'Arthur', 'Perry', 'Bram', 'Sebastiaan', 'Arkadiusz', 'Felix', 
    'Suzanne', 'Maurice', 'Mohahmed', 'Lars', 'De Wet', 'Andre', 'Arjan', 'Frans', 'Andre', 'Guleed', 'Sebastian', 'Mark', 'Anne-marijke']
old = ['Bram', 'Vincent', 'Arthur', 'Perry', 'Bram', 'Sebastiaan', 'Arkadiusz', 'Felix', 
    'Suzanne', 'Maurice', 'Mohahmed', 'Lars', 'De Wet', 'Andre', 'Arjan', 'Frans', 'Guleed', 'Sebastian', 'Mark', 'Anne-marijke', 'Karel']

newname = None
for x in range(20):
    if (new[x] == old[x]) or (new[x] == old[x - 1]) or (new[x] == old[x   1]):
        continue
    else:
        newname = new[x]

print(newname)

CodePudding user response:

If there is never going to be more than a single additional name in the list, you don't need to iterate through the whole thing - just break the loop if the names don't match. Rather than using continue for all the names that do match, just identify the one that doesn't. An if statement doesn't require an else!

for i, name in enumerate(new):
    if name != old[i]:
        newlist = [i, name]
        break

If there's a chance that there are multiple new names, though, you can use a counter to compare between the elements in the lists. The counter starts at 0 so that you compare elements at the same index value in each list, then for every new name found in new, the counter increases by one so that it's comparing to the same item in old until it matches up again.

Here I've added two additional 'Sebastiaans' next to the existing one and a 'Lars' at the end.

new = ['Perry', 'Bram', 'Sebastiaan', 'Sebastiaan', 'Sebastiaan', 'Arkadiusz', 'Felix', 'Lars']
old = ['Perry', 'Bram', 'Sebastiaan', 'Arkadiusz', 'Felix', 'Suzanne', 'Maurics', 'Mohamed']

count = 0
newlist = []

for i, name in enumerate(new):
    if name != old[i-count]:
        newlist.append([i, name])
        count  = 1

>>>[[3, 'Sebastiaan'], [4, 'Sebastiaan'], [7, 'Lars']]
  • Related