小编Gam*_*s20的帖子

通过拆分另一个列表的索引在 Python 中创建一个列表可能吗?

我尝试了以下代码,但到目前为止还没有奏效,它说我不能

names = ['John Johnson Doe', 'Jane Janis Doe']
firstnames = []

for name in names:
     firstnames.append(names[name].split(' ')[0])
print(firstnames)
Run Code Online (Sandbox Code Playgroud)

我在这里得到的错误是:

TypeError: list indices must be integers or slices, not str
Run Code Online (Sandbox Code Playgroud)

python list

2
推荐指数
1
解决办法
58
查看次数

标签 统计

list ×1

python ×1