简体   繁体   English

将列表列表转换为元组的元组

[英]List of lists into tuple of tuples

(Python) I'm a beginner and need to change my list of lists into a tuple of tuples. (Python)我是初学者,需要将我的列表列表更改为元组的元组。

 Input->  myList=[[0, 0], [0, 1], [2, 2], [1, 2]]
 Output-> ((0,0),(0,1),(2,2),(1,2))

The first thing I'm trying to do is to split my list of lists, but I keep getting an Attribute Error.我要做的第一件事是拆分我的列表列表,但我不断收到属性错误。

    myList = [[0, 0], [0, 1], [2, 2], [1, 2]]
    myList = [item[0].split(",") for item in myList]
    print(myList)

any help?有什么帮助吗?

Just convert your sublists into tuples.只需将您的子列表转换为元组。

tuple(tuple(item) for item in myList)

Using map + tuple使用map + tuple

tuple(map(tuple, myList))
#((0, 0), (0, 1), (2, 2), (1, 2))
tuple([ tuple(x) for x in myList ])
myList=[[0, 0], [0, 1], [2, 2], [1, 2]]

mytuple = tuple([tuple(elem) for elem in myList])

print(mytuple)

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

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