开发者社区> 问答> 正文

如何从数字元组列表中形成数字列表?

以下是itertools.permutations函数的输出:

a =  [(3, 1, 4, 1),
         (3, 1, 1, 4),
         (3, 4, 1, 1),
         (3, 4, 1, 1),
         (3, 1, 1, 4),
         (3, 1, 4, 1),
         (1, 3, 4, 1),
         (1, 3, 1, 4),
         (1, 4, 3, 1),
         (1, 4, 1, 3),
         (1, 1, 3, 4)...]

如何从上述数据中获取以下形式的数字列表:

[3141,3114,3411...]

目前,我只能通过以下方式获得它:

[314131143411...]

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 23:31:40 696 0
2 条回答
写回答
取消 提交回答
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    对数字列表执行简单的统计计算 数字列表的最大值、 最小值和总和 digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] min(digits) 0 max(digits) 9 sum(digits) 45 列表解析 squares = [value2 for value in range(1,11)] print(squares) for循环为for value in range(1,11),将值1~10提供给表达式value2。这里的for 语句末尾没有冒号。 结果 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 使用列表的一部分 切片 players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]  print(players[0:3]) 输出也是一个列表 [‘charles’, ‘martina’, ‘michael’] 没有指定第一个索引,从列表头开始: players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’] print(players[:4]) [‘charles’, ‘martina’, ‘michael’, ‘florence’] 输出最后三个可使用players[-3:]: 遍历切片 遍历列表的部分元素,在for循环中使用切片。 players = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’]

    print(“Here are the first three players on my team:”)  for player in players[:3]: print(player.title()) 输出 Here are the first three players on my team: Charles Martina Michael 复制列表 复制列表,创建一个包含整个列表的切片,同时省略起始索引和终止索引([:])。 始于第一个元素,终止于最后一个元素的切片,即复制整个列表。 my_foods = [‘pizza’, ‘falafel’, ‘carrot cake’]  friend_foods = my_foods[:] print(“My favorite foods are:”) print(my_foods) print("\nMy friend’s favorite foods are:") print(friend_foods) 输出 My favorite foods are: [‘pizza’, ‘falafel’, ‘carrot cake’] My friend’s favorite foods are: [‘pizza’, ‘falafel’, ‘carrot cake’] 元组 不可变的列表为元组 元组用圆括号而不是方括号 dimensions = (200, 50)  print(dimensions[0]) print(dimensions[1]) 输出 200 50 遍历元组中的所有值 dimensions = (200, 50) for dimension in dimensions: print(dimension) 输出 200 50 修改元组变量 不能修改元组的元素,可以给存储元组的变量赋值,可重新定义整个元组 dimensions = (200, 50) print(“Original dimensions:”) for dimension in dimensions: print(dimension) #修改元素 dimensions = (400, 100) print("\nModified dimensions:") for dimension in dimensions: print(dimension) #两次输出分别为 Original dimensions: 200 50 Modified dimensions: 400 100

    2020-03-24 23:41:08
    赞同 展开评论 打赏
  • 你可以试试看

    def process_data(data):
        return int(''.join(map(str,data)))
    
    out=[process_data(i) for i in a]
    # [3141, 3114, 3411, 3411, 3114, 3141, 1341, 1314, 1431, 1413, 1134,...]
    

    要么

    def process_data(data):
        num=data[0]
        for i in data[1:]:
            num=num\*0+i
        return num
    out=[process_data(i) for i in a]
    # [3141, 3114, 3411, 3411, 3114, 3141, 1341, 1314, 1431, 1413, 1134,...]
    

    回答来源:stackoverflow

    2020-03-24 23:31:48
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载