我想从列表中调出某些单词(长度超过11个字符)并找出可以通过手机上的相同按键键入的单词,例如“compurgations”和“constrictions”两者共享相同的单词代码(2667874284667)。
我能够将每个单词变成一个数字字符串,其中包含笨拙的编码但有效。
numbers = []
dial = []
for word in lowers:
if len(word)>11 and "\'" not in word:
dial.append(word)
if len(word)>11 and "\'" not in word:
worda = word.replace('a','2')
wordb = worda.replace('b','2')
wordc = wordb.replace('c','2')
wordd = wordc.replace('d','3')
worde = wordd.replace('e','3')
wordf = worde.replace('f','3')
wordg = wordf.replace('g','4')
wordh = wordg.replace('h','4')
wordi = wordh.replace('i','4')
wordj = wordi.replace('j','5')
wordk = wordj.replace('k','5')
wordl = wordk.replace('l','5')
wordm = wordl.replace('m','6')
wordn = wordm.replace('n','6')
wordo = wordn.replace('o','6')
wordp = wordo.replace('p','7')
wordq = wordp.replace('q','7')
wordr = wordq.replace('r','7')
words = wordr.replace('s','7')
wordt = words.replace('t','8')
wordu = wordt.replace('u','8')
wordv = wordu.replace('v','8')
wordw = wordv.replace('w','9')
wordx = wordw.replace('x','9')
wordy = wordx.replace('y','9')
wordz = wordy.replace('z','9')
numbers.append(wordz)
numberset = set(numbers)
然后我会搜索以查看每个数字出现的次数,如果大于1,则记录该位置并将其从另一个列表中拉出来,将它们作为元组提供。我不知道如何找出与位置相同的号码。
答案 0 :(得分:2)
建立字典可能是个好主意
charmap = { 'a' : '2', 'b' : '2', etc... }
wordz = defaultdict(list)
for word in lowers:
wordz[''.join(charmap[c] for c in word)].append(word)
for k,v in wordz.items():
if len(v) > 1:
print('{}:{}'.format(k, v))
会给你:
2667874284667:['compurgations', 'constrictions']
...
答案 1 :(得分:1)
我是这样做的:我会使用翻译表将字母映射到拨号号码,然后我会在每个号码上创建一组单词。然后我会迭代生成的dict,得到一组多个单词。
from pprint import pprint
from collections import defaultdict
dialer_table = str.maketrans({
'a':'2',
'b':'2',
'c':'2',
'd':'3',
'e':'3',
'f':'3',
'g':'4',
'h':'4',
'i':'4',
'j':'5',
'k':'5',
'l':'5',
'm':'6',
'n':'6',
'o':'6',
'p':'7',
'q':'7',
'r':'7',
's':'7',
't':'8',
'u':'8',
'v':'8',
'w':'9',
'x':'9',
'y':'9',
'z':'9',
})
dial = defaultdict(set)
for word in lowers:
if len(word) > 11 and "\'" not in word:
dial[word.translate(dialer_table)].add(word)
pprint([dialset for dialset in dial.values() if len(dialset) > 1])
答案 2 :(得分:1)
要计算某些内容出现在列表中的次数,您应该使用:
myList = ["a","b","c","a"]
myList.count("a")
2