我試圖生成元組列表,例如[('Client 0', 120),..,('Client 9', 45)]
,這是代碼中的totalitems
。我使用下面的代碼,但我收到一個錯誤,我不確定爲什麼。創建元組列表(TypeError:'int'對象不可迭代)
N = 10
value_range = np.arange(0, N, 1)
newitems = []
for i in value_range:
newvisits = ("Client %d" % i,) + random_tuple(length=6, values=(0, 15, 30, 45, 60))
newitems.append(newvisits)
totalitems = []
i = 0
for each_client in newitems:
i += 1
total = sum(each_client[1:])
newtotal = ("Client %d" % i,) + tuple(total)
totalitems.append(newtotals)
我得到的錯誤是:
newtotals = ("Client %d" % i,) + tuple(total)
TypeError: 'int' object is not iterable
我試圖找到總每個元組的totalitems
,很明顯,我需要忽略指數[0]。
有沒有人知道我要去哪裏錯了?或者有關如何改進代碼的建議?
注意: random_tuple
是非常明顯的,它返回一個長度爲6的元組,每個元素是隨機選擇的值。
在你的第二個for循環,嘗試像 '因爲我,在歷數(newitems)each_client:' – RagingRoosevelt
什麼是'random_tuple()'。你能分享你的數據看起來像什麼嗎? – AChampion