簡體   English   中英

int()參數必須是字符串,類似字節的對象或數字,而不是'list'代碼錯誤

[英]int() argument must be a string, a bytes-like object or a number, not 'list' Error in code

int() argument must be a string, a bytes-like object or a number, not 'list'在代碼行中彈出int() argument must be a string, a bytes-like object or a number, not 'list'錯誤。 我找不到解決方案。

我想念什么嗎?

L = [[13], [18], [1], [3], [4], [5], [50], [29], [30], [41]]

sum = 10 + int(L[2])

我希望sum是一個整數。

如果子數組中只有單個元素,則可以將它們展平到一個列表中,而無需更改其他代碼。

L = [[13], [18], [1], [3], [4], [5], [50], [29], [30], [41]]
L = [i for subarr in L for i in subarr]

sum = 10 + L[2]
print(L, sum)  # => [13, 18, 1, 3, 4, 5, 50, 29, 30, 41] 11

您有一個數組數組,每個數組都有一個元素。
這可能會起作用:

sum= 10 + int(L[2][0])  

或者,也許您只想構造一個數組,而每個元素都不包裝在自己的數組中;

L = [13, 18, 1, 3, 4, 5, 50, 29, 30, 41]

這是2D列表的列表。 要訪問列表的元素,您需要執行以下操作

L[2][0] # return 1
L[1][0] # return 18

為了解決您的問題,請嘗試此

sum = 10 + int(L[2][0]) # return 11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM