gpt4 book ai didi

python - 在子列表中将字符串转换为 int - python

转载 作者:太空宇宙 更新时间:2023-11-04 07:39:22 24 4
gpt4 key购买 nike

所以如果我有 100 个子列表必须将字符串替换为整数L = ['2013', 'Patrick', 'M', '2566']

我必须将 2013 年和此类数字转换为所有子列表的整数。我试过:假设每个子列表有 4 个元素newlist = [map(int,x) for x in L[0][:5]]

但显然我不断收到错误,因为无法转换“patrick”。

最佳答案

你可以简单地理解列表:)

results = [int(i) if i.isdigit() else i for i in L]

关于python - 在子列表中将字符串转换为 int - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26229330/

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