Python の配列には、Lists
&の 2 つのタイプがありTuples
ます。
list
変更可能です (つまり、必要に応じて要素を & として変更できます)
tuple
不変です (読み取り専用配列)
list
によって表される によって[1, 2, 3, 4]
tuple
表される(1, 2, 3, 4)
したがって、指定された配列list
はtuples
!
リストでタプルをネストできますが、タプルでリストをネストすることはできません。
これはよりpythonicです -
items = [('this', 5, 'cm'), ('that', 3, 'mm'), ('other', 15, 'mm')]
found_items = [list(item) for item in items]
for i in range(len(found_items)):
print (found_items[i])
new_value = int(input ("Enter new value: "))
for i in range(len(found_items)):
recalculated_item = new_value * found_items[i][1]
print (recalculated_item)
上記のコードからの出力(入力を 3 として受け取る)
['this', 5, 'cm']
['that', 3, 'mm']
['other', 15, 'mm']
15
9
45
更新:このコメントとこの回答をフォローアップして、上記のコードを更新しました。