def caculate(ITEMS, need, i): #i,need为int型ITEMS为一个二维的元组
temp1 = list(ITEMS)
temp1[i] = list(temp1[i])
temp1[i][INDEX_ITEM_TOTAL_QAUNTITY] = list(temp1[i][INDEX_ITEM_TOTAL_QAUNTITY])
temp1[i][INDEX_ITEM_TOTAL_QAUNTITY] = temp1[i][INDEX_ITEM_TOTAL_QAUNTITY]-need
temp1[i][INDEX_ITEM_TOTAL_QAUNTITY] = tuple(temp1[i][INDEX_ITEM_TOTAL_QAUNTITY])
ITEMS[i] = tuple(temp1[i])
ITEMS = tuple(temp1)
运行结果如下:
temp1[i][INDEX_ITEM_TOTAL_QAUNTITY] = list(temp1[i][INDEX_ITEM_TOTAL_QAUNTITY])
TypeError: 'int' object is not iterable
问题出在i上吗,我该如何进行更改呢?
或者,该代码的目的是进行数量的加减,如何在元组情况下更改其中某项元素(例如库存数量的增减)?
元组如下:
ITEMS = (("MacBook Pro", "Apple", 2), ("AC1200 Wireless Adapter", "ALFA Network", 4), ("Jetson Nano Developer Kit", "NVIDIA", 3), ("WRT1900AC Dual-Band Wi-Fi Router", "Linksys", 2), ("RoboMaster EP", "DJI", 2))