[英]How to split up elements of a list that occupy the same index position?
I am new to Python and can not seem to find a solution to what I am sure is a simple problem. 我是Python的新手,似乎无法找到我确定是简单问题的解决方案。 I have a list of three elements but they occupy a single position. 我列出了三个要素,但它们只占一个位置。 How can I split them up to store them in separate positions in a list? 如何拆分它们以将它们存储在列表中的不同位置?
Code: 码:
li = [(2,3,6)]
Currently output 当前输出
li[0] = (2,3,6)
My desired output would be 我想要的输出是
li = [2,3,6]
So that li[0] = 2, li[1] = 3, li[2] = 6
因此, li[0] = 2, li[1] = 3, li[2] = 6
Any help is much appreciated, thanks! 非常感谢任何帮助,谢谢!
In your case, you can just convert the li[0] elements to list store them in separate variables. 在您的情况下,您可以只转换li [0]元素以列出将它们存储在单独的变量中。 Or save it to the same one. 或将其保存到同一个。
For ex:- 例如:
li = [(1,2,3)]
li = list(li[0])
print(li)
output 输出
Output:
[1,2,3]
I have a list of three elements but they occupy a single position 我列出了三个要素,但它们只占一个位置
Actually, here 其实在这里
li = [(2,3,6)]
li
is not "a list of three elements", it's "a list of a single element", which single element is a tuple
of three elements. li
不是“三个元素的列表”,而是“单个元素的列表”,其中单个元素是三个元素的tuple
。
If you want to make a list of three elements from this, you have to 如果要从中列出三个元素,则必须
1/ extract the tuple from the list. 1 /从列表中提取元组。 It's the first element so it's at index zero: 它是第一个元素,因此它的索引为零:
elem = li[0]
2/ build a list from elem
: 2 /从elem
建立清单:
li = list(elem)
or, in a single statement: 或者,在一个语句中:
li = list(li[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.