Skip to content Skip to sidebar Skip to footer

Python: Matching Values From One List To The Sequence Of Values In Another List

Really need help. I've confused myself with loops and hit a brick wall. I have two lists. e_list = [('edward', '1.2.3.4.'), ('jane','1.2.3.4.'), ('jackie', '2.3.4.10.')...] and

Solution 1:

for name, digits in e_list:
    shortest = sorted([d for l, d in a_list if digits.startswith(d)], key=len)[0]
    print name, digits, shortest

Solution 2:

Sort a_list first, and then do the loop to check.

a_list.sort(key=lambda x: x[1])
for e in e_list:
    e_key = e[1]
    for a in a_list:
        a_key = a[1]
        if a_key.startswith(e_key):
            print a_key, "in"break

After the sort, the short digit string in a_list will come first, so in the loop you only need to find the first match.

Post a Comment for "Python: Matching Values From One List To The Sequence Of Values In Another List"