In Python: 在表示 X, Y 点的元组列表中,访问特定 X 或 Y 的语法是什么?

In Python: In a list of tuples that represent X, Y points, what is the syntax to access a particular X or Y?

例如如果

list = [{0: 1}, {0: 2}, {1: 0}, {1: 2}, {2: 0}, {2: 1}, {2: 2}]

获取第二项的 xy 的语法是什么?

x = list[1] ?
y = list[1] ?

您显示的列表是:

[{0: 1}, {0: 2}, {1: 0}, {1: 2}, {2: 0}, {2: 1}, {2: 2}]

这是 dictslist,每个都有一个元素。对于第二项(在索引 1 处),字典有一对 key/value,键 0 和值 2。要访问它,您可以这样做:

        myList = [{0: 1}, {0: 2}, {1: 0}, {1: 2}, {2: 0}, {2: 1}, {2: 2}]
        myDict = myList[1]
        key, value = next(iter(myDict.items()))
        print(key, value)

如果您想要的是 tupleslist,那将类似于:

[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)]

您可以按如下方式访问 list 中的第二个 tuple

        myList = [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1), (2, 2)]
        x = myList[1][0]
        y = myList[1][1]
        print(x, y)

或者,您可以使用这种更简洁的赋值语法:

        x, y = myList[1]