1

I want to call certain words from a list (greater than 11 characters long) and find out what words can by typed using the same key presses on a cell phone, for example "compurgations" and "constrictions" both share the same code (2667874284667).

I was able to make each word into a number string with the clumsily coded but effective.

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)   

I was going to then search to see how many times each number appears, and if greater than 1, log the location and pull together it from the other list, providing them as a tuple. I don't see how I can find out which share the same number with location.

3 Answers 3

2

It would probably be a good idea to build a dictionary

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))

Will give you:

2667874284667:['compurgations', 'constrictions']
...
5
  • I like this, as its pretty compact. However as I reach words[[charmap[c] for c in word]].append(word) I get TypeError: string indices must be integers, not list.
    – Krilion
    Sep 18, 2013 at 4:30
  • My mistake, charmap needed to be strings and join the result.
    – AChampion
    Sep 18, 2013 at 4:37
  • TypeError: sequence item 0: expected string, int found Sorry, never used defaultdict and so I'm semi at a loss of what's going on. I used c elseware, but changed that and still get the error.
    – Krilion
    Sep 18, 2013 at 4:43
  • Make sure charmap is 'a' : '2' not 'a' : 2
    – AChampion
    Sep 18, 2013 at 4:51
  • AH!. Works great, any idea on how I'd be able to print up ONLY those with multiple variations?
    – Krilion
    Sep 18, 2013 at 4:55
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])
1

To count how many times something appears in a list you should use:

myList = ["a","b","c","a"]
myList.count("a")
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.