gpt4 book ai didi

python - 修改python列表中的所有元素,并将类型从字符串更改为整数

转载 作者:行者123 更新时间:2023-12-03 16:10:03 25 4
gpt4 key购买 nike

我有一个看起来像['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/

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