I'm using the following: http://deron.meranda.us/data/nicknames.txt which has the nicknames for most of the names. I'm using it as follows:
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)
This returns a dictionary where the actual names as keys and all their nicknames as a list of values for that key.
Now, when I have a
namelist = ['KEN', 'KENNY', 'KENNETH', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
then I want to output only the different names ie, ['KENNETH', 'MITCHELL', 'ELIZABETH']
, which I'm able to do by:
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)
However, I also want it to deal the case like ['KEN', 'KENNY', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
, this should output ['KEN', 'MITCHELL', 'ELIZABETH']
ie, the two nicknames 'KEN' and 'KENNY' belong to the same key so, I want to consider them as one and have only one of them in the final list. Also, if I have a namelist as ['KENNETH, 'ZHAO', 'MITCH', 'MITCHELL']
then it should output: ['KENNETH', 'ZHAO', 'MITCHELL']
ie, for such names that do not occur in the dictionary at all (in either key or value), then output list should still have that name. How do I get that?
UPATE:
outdict = {'KENNETH': ['KEN', 'KENNY'], 'MITCHELL': ['MITCH'], 'ELIZABETH' : ['LIZ', 'LIZZIE'], .....}
namelist1 = ['KEN', 'KENNY', 'KENNETH', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']
output1 = `['KENNETH', 'MITCHELL', 'ELIZABETH']`
I'm getting the above output from the code I have put.
However, I want to be able to get the following outputs also when the namelists are as follows:
namelist2 = ['KEN', 'KENNY', 'MITCH', 'MITCHELL', 'LIZ', 'LIZZIE', 'ELIZABETH']`
output2 = `['KEN', 'MITCHELL', 'ELIZABETH']`
namelist3 = ['KENNETH, 'ZHAO', 'MITCH', 'MITCHELL']`
output3 = `['KENNETH', 'ZHAO', 'MITCHELL']`
Got the answer myself:
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)
try this,
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)
if its not put your dict object
Adding the lists to a dict and then returning the keys works, but there has to be a better way.
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())
Outputs
['ELIZABETH', 'MITCHELL', 'KENNETH']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.