简体   繁体   English

如何拆分> 1位整数列表并从该数字创建列表? 蟒蛇

[英]How to split >1digit integers list and create a list from that digits? Python

Say I have list a = [23, 2] . 说我有a = [23, 2]列表a = [23, 2] I need to split every element in list and then combine them to one list. 我需要拆分列表中的每个元素,然后将它们组合到一个列表中。 So result be like a = [2, 3, 2] What I have for now is: 所以结果就像a = [2, 3, 2]我现在拥有的是:

list = [int(i) for i in str(map([], map(lambda x: x*2, list(reversed(numlist))[1::2])))]

Where map(lambda x: x*2, list(reversed(numlist))[1::2]))) is the list of even indexed numbers (2, 4, 6...) each multiplied by 2. 其中map(lambda x: x*2, list(reversed(numlist))[1::2])))是偶数索引数字(2、4、6 ...)的列表,每个数字都乘以2。

It gives me: ValueError: invalid literal for int() with base 10: '<' on this line. 它给我: ValueError: invalid literal for int() with base 10: '<'该行的ValueError: invalid literal for int() with base 10: '<'

>>> map(int, ''.join(map(str, a)))
[2, 3, 2]

Using two level List Comprehension: 使用两级列表理解:

a = [23, 2]
digits = [ int(x) for num in a for x in str(num) ]

You can first change list to string then change this string back to integer list. 您可以先将列表更改为字符串,然后将此字符串更改回整数列表。 Here is an example. 这是一个例子。

a = [23, 2]
a = [int(j) for j in ''.join([str(i) for i in a])]
print(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM