到目前为止,我还没有找到任何有助于在网上解释这一点的内容,我认为这一定是我的一些问题。问题是,在Python 3.4中使用Spyder2作为IDE我在列表中不断出现语法错误,我需要帮助理解它。
所以这就是我的尝试。
当我保持列表相对较小时,它似乎工作正常,但一旦超过76个字符就开始给我这个错误:
runfile('C:/Users/ghost/.spyder2-py3/temp.py', wdir='C:/Users/ghost/.spyder2-py3') File "C:/Users/ghost/.spyder2-py3/temp.py", line 7 alphabet = [a = 0, b = 1, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, SyntaxError: invalid syntax
代码
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
alphabet = [a = 0, b = 1, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8,
i = 9, j = 10, k = 11, l = 12, m = 13, n = 14, o = 15, p = 16,
q = 17, r = 18, s = 19, t = 20, u = 21, v = 22, w = 23, y = 24,
x = 25, z = 26]
print(alphabet)
答案 0 :(得分:5)
这是不正确的语法。对于[]
,您有list
,但是您要将每个值分配给另一个值,这更像是dict
,因此不起作用。您可以通过将其发送到dict()
函数来创建字典来解决最小的更改:
>>> alphabet = dict(a = 0, b = 1, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8,
... i = 9, j = 10, k = 11, l = 12, m = 13, n = 14, o = 15, p = 16,
... q = 17, r = 18, s = 19, t = 20, u = 21, v = 22, w = 23, y = 24,
... x = 25, z = 26)
>>> alphabet['b']
1