gpt4 book ai didi

python - 列表访问 e[-1] 可以,但 e[1] 不在 Python 中

转载 作者:行者123 更新时间:2023-11-28 21:20:13 27 4
gpt4 key购买 nike

<分区>

在Python中,我定义了

string = ("car-automobile, gem-jewel, journey-voyage, boy-lad, coast-shore, "
"asylum-madhouse, magician-wizard, midday-noon, furnacestove, food-fruit, "
"bird-cock, bird-crane, tool-implement, brother-monk, ladbrother, "
"crane-implement, journey-car, monk-oracle, cemetery-woodland, foodrooster, "
"coast-hill, forest-graveyard, shore-woodland, monk-slave, coast-forest, "
"lad-wizard, chord-smile, glass-magician, rooster-voyage, "
"noon-string".split(', '))
test = [i.split('-') for i in string]

下面的代码会导致错误:

[e[1] for e in test]
Traceback (most recent call last):
File "<pyshell#136>", line 1, in <module>
[e[1] for e in test]
IndexError: list index out of range

但下面的代码有效

[e[-1] for e in test]

为什么会这样?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com