Python检查两个列表中的重复值和空值并映射它们

时间:2018-05-14 07:09:35

标签: python python-3.x

我有两个名称和数字列表,其中包含一些空白和重复的条目

phone = ['918222822','92929129','','92929129','','12345678','12345678','1727398','']
name  = ['jhon', 'ele', '' , 'ele', '' , 'rock','rock','','burh']
  1. 如果您看到大多数数字都映射到名称。
  2. 有一种情况,我为名称空白编号输入,反之亦然。
  3. 这是迄今为止所尝试的内容:

    phone = ['918222822','92929129','','92929129','','12345678','12345678','1727398','']
    name  = ['jhon', 'ele', '' , 'ele', '' , 'rock','rock','','burh']
    phone = set(phone)
    name = set(name)
    for na, ph in zip(name,phone):
        if na is None:
           na = ph
        elif ph is None:
           #do something
           pass
        print(na,ph)
    

    我想要输出的是:

    输出

    jhon  918222822
    ele   92929129
    rock  12345678
    1727398   1727398  # if number found but not name then put number in name
                       # if only name found but no corresponding number then delete entries in both list   
    

3 个答案:

答案 0 :(得分:1)

phone = ['918222822','92929129','','92929129','','12345678','12345678','1727398','']
name  = ['jhon', 'ele', '' , 'ele', '' , 'rock','rock','','burh']

res = set([i for i in zip(phone, name) if i[0]])
res = dict((i[0], i[1]) if i[1] else (i[0], i[0]) for i in res)
print(res.keys())   #phone
print(res.values())  #name

<强>输出:

['1727398', '918222822', '12345678', '92929129']
['1727398', 'jhon', 'rock', 'ele']

答案 1 :(得分:1)

假设两个列表具有相同的长度,只需迭代它们并按照您的描述匹配它们的条目:

from pprint import pprint

phone = ['918222822','92929129','','92929129','','12345678','12345678','1727398','']
name = ['jhon', 'ele', '' , 'ele', '' , 'rock','rock','','burh']

result = {}
removed = []
for idx, thephone in enumerate(phone):
    # If name is empty string, use phone as name
    thename = name[idx] if len(name[idx]) else thephone

    # If one of both still is empty string, skip the entry
    if not len(thename) or not len(thephone):
        # Store removed names for later processing;
        # Note that this also stores empty names that are pruned
        # Add conditional `if len(thename):` if they shall be skipped
        removed.append(thename)
        continue

    # If not already present, store values in dict
    if thename not in result.keys():
        result[thename] = thephone

pprint(result)
pprint(removed)

结果是:

{'1727398': '1727398',
 'ele': '92929129',
 'jhon': '918222822',
 'rock': '12345678'}
['', '', 'burh']

答案 2 :(得分:0)

您可以使用defaultdict映射与姓名对应的所有电话号码:

phone = ['918222822','92929129','','92929129','','12345678','12345678','1727398','']
name  = ['jhon', 'ele', '' , 'ele', '' , 'rock','rock','','burh']

phones = collections.defaultdict(list)
phones = collections.defaultdict(set)
for i, n in enumerate(name):
    phones[n].add(phone[i])

for n in phones:
    print(n , ":", ', '.join([ ph if ph != '' else "''" for ph in phones[n]]))

输出是:

jhon : 918222822
ele : 92929129
 : '', 1727398
rock : 12345678
burh : ''

输出可能不是您想要的,但由于它现在是一个映射,因此很容易对其进行后期处理。