gpt4 book ai didi

python - 在 Python 中总结字符串列表

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

我有一个包含这种列表的列表:

l = [['709', '651', '750', '806', '850', '759', '801'],
['603', '551', '600', '555', '559', '655', '700'],
['603', '-', '-', '601', '-', '-', '-']]

我想要一个包含每个单独列表总和的新列表。

在这种情况下,结果应该是:

[5326, 4223, 1204]

这可能吗?

最佳答案

lists = [
['709', '651', '750', '806', '850', '759', '801'],
['603', '551', '600', '555', '559', '655', '700'],
['603', '-', '-', '601', '-', '-', '-']
]

lists_sum = [sum(int(i) for i in l if i != '-') for l in lists]

关于python - 在 Python 中总结字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23557240/

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