list返回None,省略值

时间:2016-12-20 23:27:50

标签: python list dictionary

我试图返回字母的值和位置。将此作为普通for循环运行工作正常。当我把它变成一个看起来很糟糕的功能时。

这是它的输出:

dict = {'a': 1, 'b': 2 ... 'z': 26}

list1 = []
list2 = []

def plot(word):
    counter = 0
    for i in word:
        y = dict.get(i)
        list1.append(y) #keeps printing None for the first letters
        counter += 1
        x = counter
        list2.append(x)
    print list1
    print list2
    r = zip(list1, list2)
    print r

t = raw_input('Enter word: ')
Enter word: Hello

plot(t)

Output:
[None, None, 5, 12, 12, 15]
[1, 2, 3, 4, 5]
[(None, 1), (None, 2), (5, 3), (12, 4), (12, 5)]

1 个答案:

答案 0 :(得分:2)

我认为问题是你正试图绘制大写字母。我只需更改for循环以迭代小写。

for i in word.lower():
    y = dict.get(i)
    list1.append(y) #keeps printing None for the first letters
    counter += 1
    x = counter
    list2.append(x)