简体   繁体   English

如何拆分列表中占据相同索引位置的元素?

[英]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.

相关问题 如何索引列表中的项目并从另一个列表中提取相同的索引 position - How is it possible to index an item in a list and pull up the same index position from another list 如何在没有循环的情况下在同一索引 position 中用 python 中不同大小的另一个列表的元素替换列表的元素 - How to replace elements of a list with elements of another list of a different size in python in the same index position without a loop 如何在 Python 中拆分由 ],[ 分隔的列表元素 - How to split up elements of a list separated by ],[ in Python 在具有相同元素的列表的索引 position 上查找元素 - Finding element on index position of a list which has same elements 如何删除2个不同列表中相同索引的元素? - How to delete elements of the same index in 2 different list? 如何在列表中拆分索引字符串并存储在同一列表中? - How to split string of index in list and store within same list? 如何将一个索引的列表拆分为单个元素? - How do I split the list of one index into individual elements? 如何根据元素的长度将文本列表分成多个部分 - How to split a list of text up into sections based on the length of the elements 如何以随机索引作为起始位置迭代/压缩多个列表的元素 - how to iterate/zip elements of multiple list with random index as start position 如何拆分列表的元素? - How to split elements of a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM