【问题标题】:convert string elements in list as integers in python将列表中的字符串元素转换为python中的整数
【发布时间】:2017-08-21 15:01:41
【问题描述】:

我有一个名为 newlist 的列表

newlist=[['24,4,17,46,0,43'], ['11,43,17'], ['33,17,43,4'], ['74,21'],['21,43,43,74,68,21']]

我需要将每个列表元素转换为整数。即

newlist=[[24,4,17,46,0,43], [11,43,17], [33,17,43,4], [74,21], [21,43,43,74,68,21]].

谁能帮帮我。

【问题讨论】:

    标签: string list int


    【解决方案1】:

    Python 3:

    newlist=[['24,4,17,46,0,43'], ['11,43,17'], ['33,17,43,4'], ['74,21'],['21,43,43,74,68,21']]
    mylist = list(map(lambda x : list(map(int, x[0].split(','))) , newlist))
    print(mylist)
    

    Python 2:

    newlist=[['24,4,17,46,0,43'], ['11,43,17'], ['33,17,43,4'], ['74,21'],['21,43,43,74,68,21']]
    mylist = map(lambda x : map(int, x[0].split(',')) , newlist)
    print mylist
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2023-03-06
      • 1970-01-01
      • 2011-01-08
      相关资源
      最近更新 更多