1

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

4

3 回答 3

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']
...
于 2013-09-18T04:04:31.483 回答
1

Here's how I'd do it: I'd use a translation table to map the letters to dialer numbers, then I'd create sets of words at each number. Then I'd iterate over the resulting dict to get the ones with a set of more than one word.

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])
于 2013-09-18T04:01:26.763 回答
1

要计算列表中出现的次数,您应该使用:

myList = ["a","b","c","a"]
myList.count("a")
2
于 2013-09-18T04:52:29.060 回答