gpt4 book ai didi

python - 定位列表中重复项的位置及其位置

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:49 25 4
gpt4 key购买 nike

我想调用列表中的某些词(长度超过 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,则记录位置并将其从另一个列表中拉到一起,将它们作为元组提供。我不知道如何找出哪些号码与位置共享相同的号码。

最佳答案

建立字典可能是个好主意

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']
...

关于python - 定位列表中重复项的位置及其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18863405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com