3 回答

TA貢獻1840條經驗 獲得超5個贊
只需使用tuple_list[listindex][tupleindex], wherelistindex是列表tupleindex中的位置,是元組中的位置。對于您的示例,請執行以下操作:
loc = tuple_list[1][1]
請注意元組是不可變的集合。如果要更改它們,則應改用列表。但是,具有元組值的變量仍然可以重新分配給新的元組。例如,這是合法的:
x = ('a', 'b', 'c')
x = (1, 2, 3)
但這不是:
x = ('a', 'b', 'c')
x[0] = 1

TA貢獻1884條經驗 獲得超4個贊
元組具有與列表相同的索引,因此您可以[0]在列表中獲取以下元組的索引。然而,一個問題是元組不能被修改,因此你必須為每個賦值生成一個新的元組。
例如:
tuple_list = [(a, b), (c, d), (e, f), (g, h)]
for x in range(0, len(tuple_list) - 1): # Go until second to last tuple, because we don't need to modify last tuple
tuple_list[x] = (tuple_list[x][0],tuple_list[x+1][0]) # Set tuple at current location to the first element of the current tuple and the first element of the next tuple
會產生想要的結果
添加回答
舉報