【问题标题】:How to convert a list of list of strings into integers如何将字符串列表的列表转换为整数
【发布时间】: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】:
    # 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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2015-06-19
      • 2015-01-14
      • 2021-06-18
      • 2020-01-26
      相关资源
      最近更新 更多