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

标签 python list

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

相关文章:

python - 与 celery 中的任务实例相关的属性

python - OSError : [WinError 10013] error in Windows 10

Excel VBA : Named range + string value as validation list?

python - a[ :]=b and a=b[:] 之间有什么区别

c# - 在 C# 中查找和删除元组列表中的重复项

python - 如何更新python中的变量列表?

python os.listdir 遇到 OSError : [Errno 13] Permission denied

python - 在 Matplotlib 中创建颜色图图例

python - 在具有给定字母的所有可能的 4 字符字符串中查找 4 字符字符串索引的有效方法

python - 在列表中查找递增数字组