How can I compare 2 indexes in same list in Python?

时间:2018-09-18 20:29:11

标签: python

code_length = len(morse_list)
morse_length = len(morse_code)

for i in range(code_length):
    for j in range(morse_length):
        if morse_list[i] == morse_code[j][1]:
            translated += morse_code[j][0]

    if morse_list[i] == '':
        translated += ' '

    elif morse_list[i] and morse_list[i+1] == '': <<<<<<<<<<<<
        translated += '\n'

IndexError: list index out of range. What I am trying to do is, if there are two enters in-a-row on user input, I want to put enter(\n) which is after finish the N sentence. I am sure there is the better way but I can't figure it out. Any suggestions, please?

Edit for example, if in the list

['.-', '-...', '-.-.', '-..', '', '', '.', '..-.', '', '', '--.'],
output: abcd\n ef\n g

Edit2,

morse_code = (
   ('a', '.-'), ('b', '-...'), ('c', '-.-.'), ('d', '-..'),
   ('e', '.'), ('f', '..-.'), ('g', '--.'), ('h', '....'), ('i', '..'),
   ('j', '.---'), ('k', '-.-'), ('l', '.-..'), ('m', '--'), ('n', '-.'),         
   ('o', '---'), ('p', '.--.'), ('q', '--.-'), ('r', '.-.'),
   ('s', '...'), ('t', '-'), ('u', '..-'), ('v', '...-'), ('w', '.--'),
   ('x', '-..-'), ('y', '-.--'), ('z', '--..')
)

morse_list = [] # user input words

3 个答案:

答案 0 :(得分:2)

Your morse_list [i + 1] doesn't exist if i == code_length - 1, since you then try to retrieve morse_list [code_length].

The indexes only run from 0 to code_length - 1.

答案 1 :(得分:1)

我对此不满意,但是它可以正常工作并且会得到输出,有人要上学我哈哈

morse_code = ( 
   ('a', '.-'), ('b', '-...'), ('c', '-.-.'), ('d', '-..'),
   ('e', '.'), ('f', '..-.'), ('g', '--.'), ('h', '....'), ('i', '..'),
   ('j', '.---'), ('k', '-.-'), ('l', '.-..'), ('m', '--'), ('n', '-.'),             
   ('o', '---'), ('p', '.--.'), ('q', '--.-'), ('r', '.-.'),
   ('s', '...'), ('t', '-'), ('u', '..-'), ('v', '...-'), ('w', '.--'),
   ('x', '-..-'), ('y', '-.--'), ('z', '--..')
)

test = ['.-', '-...', '-.-.', '-..', '', '', '.', '..-.', '', '', '--.']

output = []
for index, item in enumerate(test):
    for a in morse_code:
        if item == a[1]:
            output.append(a[0])
        elif item == '' and test[index +1] == '':  
            output.append('\n')
            break
        else:
            pass
print(''.join(output))
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 morse.py 
abcd
ef
g

答案 2 :(得分:1)

我会将莫尔斯电码放入字典中。

morse = {'.-': 'a',
         '-...': 'b',
         '-.-.': 'c',
         '-..': 'd',
         '': ' '}

morse_list = ['.-', '-...', '-.-.', '-..', '', '', '.-', '-...']
message = ''

for item in morse_list:
    message += morse[item]
    message = message.replace('  ', '\\n ')

print(message)

输出:abcd \ n ab

我明白了。这是列表版本。如果我们可以将'if'放入'morse_code'中,则可以从循环中删除'if'。

morse_code = (
   ('a', '.-'), ('b', '-...'), ('c', '-.-.'), ('d', '-..'),
   ('e', '.'), ('f', '..-.'), ('g', '--.'), ('h', '....'), ('i', '..'),
   ('j', '.---'), ('k', '-.-'), ('l', '.-..'), ('m', '--'), ('n', '-.'),         
   ('o', '---'), ('p', '.--.'), ('q', '--.-'), ('r', '.-.'),
   ('s', '...'), ('t', '-'), ('u', '..-'), ('v', '...-'), ('w', '.--'),
   ('x', '-..-'), ('y', '-.--'), ('z', '--..')
)

morse_list = ['.-', '', '-...', '-.-.', '-..', '', '', '.-', '-...']

message = ''
for item in morse_list:
    if not item:
        message += ' '
        continue
    message += next(filter(lambda m: m[1] == item, morse_code))[0]

message = message.replace('  ', '\n')
print(message)