【发布时间】:2022-12-18 16:52:16
【问题描述】:
这是我有 [['1.0\n'],['2.0\n'],['3.0\n']] 的列表,我想将它们转换为整数 1 2 3 而不用逗号分隔和 \n .
我不确定如何进行这种转换,因为列表中有一个列表,而且我真的不知道如何完全摆脱 \n 。谢谢。
【问题讨论】:
标签: python python-3.x list
这是我有 [['1.0\n'],['2.0\n'],['3.0\n']] 的列表,我想将它们转换为整数 1 2 3 而不用逗号分隔和 \n .
我不确定如何进行这种转换,因为列表中有一个列表,而且我真的不知道如何完全摆脱 \n 。谢谢。
【问题讨论】:
标签: python python-3.x list
# Sample list of list of strings
lst = [['1.0
'], ['2.0
'], ['3.0
']]
# Convert the list of list of strings into a list of integers
result = []
for sublist in lst:
for string in sublist:
# Convert the string into a floating-point number, then into an integer
result.append(int(float(string.strip())))
print(result) # Output: [1, 2, 3]
【讨论】: