我使用以下内容:http://deron.meranda.us/data/nicknames.txt,其中包含大多数名称的昵称。我按如下方式使用它:
def load_names():
with open('file.txt', 'r') as infile:
outdict = collections.defaultdict(list)
for i, line in enumerate(infile.readlines()):
tmp = line.strip().split('\t')
outdict[tmp[1]].append((tmp[0]))
return dict(outdict)
这会返回一个字典,其中实际名称为键,所有昵称都作为该键的值列表。
现在,当我有一个
namelist = ['KEN', 'KENNY', 'KENNETH', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
然后我想只输出不同的名称,即['KENNETH', 'MITCHELL', 'ELIZABETH']
,我可以通过以下方式完成:
l = load_names()
temp =[]
for i in namelist:
v = l.get(i)
if v is not None:
l3 = [x for x in namelist if x not in v]
temp.append(l3)
print(temp)
s = set.intersection(*map(set,temp))
s = list(s)
print(s)
但是,我还希望它处理像['KEN', 'KENNY', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
这样的情况,这应该输出['KEN', 'MITCHELL', 'ELIZABETH']
,即两个昵称' KEN'和' KENNY'属于同一个键所以,我想把它们视为一个,并且在最终列表中只有一个。此外,如果我有一个名单作为['KENNETH, 'ZHAO', 'MITCH', 'MITCHELL']
,那么它应该输出:['KENNETH', 'ZHAO', 'MITCHELL']
即,对于根本没有出现在字典中的名称(在键或值中),输出列表仍然应该有这个名字。
我怎么做到的?
UPATE:
outdict = {'KENNETH': ['KEN', 'KENNY'], 'MITCHELL': ['MITCH'], 'ELIZABETH' : ['LIZ', 'LIZZIE'], .....}
namelist1 = ['KEN', 'KENNY', 'KENNETH', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
output1 = `['KENNETH', 'MITCHELL', 'ELIZABETH']`
我从我提供的代码中获得了上述输出。
但是,当名单列表如下时,我希望能够获得以下输出:
namelist2 = ['KEN', 'KENNY', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']`
output2 = `['KEN', 'MITCHELL', 'ELIZABETH']`
namelist3 = ['KENNETH, 'ZHAO', 'MITCH', 'MITCHELL']`
output3 = `['KENNETH', 'ZHAO', 'MITCHELL']`
我自己得到了答案:
l = load_names()
temp =[]
e = {}
for n in namelist:
if n in l.keys():
temp.append(n)
for ix in namelist:
for key, i in l.items():
if key in namelist:
continue
else:
b=0
if ix in i:
b=1
f=0
for x in temp:
if x in i:
f=1
break
if f == 0:
temp.append(ix)
break
b=0
for k2, loc in l.items():
if ix in loc:
b=1
break
elif ix == k2:
b=1
break
if b == 0:
temp.append(ix)
break
print(temp)
答案 0 :(得分:0)
试试这个,
def getOddout(st):
out = []
for s in st:
if not any([s in r for r in st if s != r]):
out.append(s)
return out
namelist = ['KEN', 'KENNY', 'KENNETH', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
print getOddout(namelist)
如果它没有放你的dict对象
答案 1 :(得分:0)
将列表添加到dict然后返回密钥有效,但必须有更好的方法。
l = load_names()
e = {}
for k, v in l.items():
for n in namelist:
if n in v and k not in e:
e[k] = v
print('dict', e.keys())
输出
['ELIZABETH', 'MITCHELL', 'KENNETH']