我有一个看起来像['3,2,4', '21,211,43', '33,90,87']
的数字列表
我猜此时元素被认为是字符串。
我想删除倒置的昏迷,并列出所有这些数字。
预期输出为[3,2,4, 21,211,43, 33,90,87]
另外,我想知道元素的类型是否从字符串转换为整数。
有人请帮助我!
最佳答案
关于以下内容:
result = []
# iterate the string array
for part in ['3,2,4', '21,211,43', '33,90,87']:
# split each part by , convert to int and extend the final result
result.extend([int(x) for x in part.split(",")])
print(result)
输出:$ python3 ~/tmp/so.py
[3, 2, 4, 21, 211, 43, 33, 90, 87]
关于python - 修改python列表中的所有元素,并将类型从字符串更改为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64884562/