![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
What's wrong with this code?
I'm writing a function that takes a number (eg, 2345) and selects those names from a pre-defined list of names (in a text file) that can be formed by replacing each number with one of three corresponding letters, shown below:
2: ['A', 'B', 'C'] 3: ['D', 'E', 'F'] 4: ['G', 'H', 'I'] 5: ['J', 'K', 'L'] 6: ['M', 'N', 'O'] 7: ['P', 'R', 'S'] 8: ['T', 'U', 'V'] 9: ['W', 'X', 'Y'] # works, but slow def lettercombos(num): if (len(num) == 1): return numdict[int(num)] for n in num: ret = [] for l in numdict[int(n)]: for x in lettercombos(num[1:]): ret.append(l + x) return ret # does not work for i in range(len(num)): letters = numdict[int(num[i])] for name in namelist: if name[i] not in letters: namelist.remove(name) Thanks for your help. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() |
I think the problem may be that it's iterating through a list that you're changing.
Create a temporary copy of the list before, then copy back after like so: # does not work
for i in range(len(num)): # i is the index of each digit
letters = numdict[int(num[i])]
templist = namelist[:] # create a direct copy of the list rather then referencing it
for name in namelist:
if name[i] not in letters:
templist.remove(name)
namelist = templist[:]If that doesn't work, please post a runnable version of the code to demonstrate the problem. Right now I don't know how to piece your snippets together. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I've solved the problem by rewriting the function using filter():
for i in range(len(num)): letters = numdict[int(num[i])] namelist = filter(lambda w: w[i] in letters, namelist) ![]() EDIT: Ooops, Sane: It looks like you were right. I was still trying to remove from the list I was iterating through... The working function is as follows: nc = list(namelist) for i in range(len(num)): letters = numdict[int(num[i])] for name in nc: if name in namelist and name[i] not in letters: namelist.remove(name) ![]() |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
This question can be answered simply by this statement.... *It is Python*
|
|
|
|
|
|
#5 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
Are you really that airheaded?
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
I guess... :o
BTW, why do I have to confirm "name in namelist"? Since nc is a copy of namelist, they should both contain the same names, but I get an error without this check. (Maybe the list contains duplicates? ... but that shouldn't be a problem... should it?) |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
Wow, it was a joke about the actual language...
|
|
|
|
|
|
#9 |
|
Expert Programmer
|
Ohhhhhh... I thought you wrote "Is it Python", not "It is Python"...
![]() |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() ![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|