Working with Lists and tuples

萝らか妹 提交于 2020-01-17 15:03:32

问题


My data looks like:

X=[1,2,3,4]

But I need it to look like:

Y=[(1,2,3,4)]

How does one do this in python?


回答1:


Try this:

l = [1,2,3,4]
l2 = [tuple(l)]



回答2:


To do this is simple

>>> X = [1,2,3,4]
>>> [tuple(X)]
[(1, 2, 3, 4)]

Convert X to a tuple and wrap it in a list. This is only one of probably many ways to do this. It doesn't seem like a very useful thing, so if you could explain why you want to do this, we may be able to suggest some more useful code for you.



来源:https://stackoverflow.com/questions/42639835/working-with-lists-and-tuples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!