我是 python 新手,正在使用 Florian Dedov 撰寫的 The Python Bible 7 in 1 來學習。我一直做得很好,但我遇到了本書中描述的串列函式的障礙,需要幫助。我不知道如何使用書中的list(element)函式,它被描述為將元素型別轉換為 list。我的嘗試和錯誤如下
數字= [10,22,61,29]
print(numbers(29)) 這給了我型別錯誤'list' object is not callable
您將如何正確使用此功能?
uj5u.com熱心網友回復:
print(numbers(29))需要是print(numbers[29])
圓括號(i)要求解釋器呼叫帶有引數的函式i,方括號[i]是在具有索引的物件中查找索引i,例如串列。
uj5u.com熱心網友回復:
您在示例中使用了錯誤的函式。本書將List(iterable)/List()描述為List(element),因此這意味著正確使用本書所描述的“List(element)”函式是將可迭代的(如集合)轉換為串列. 本書將這種轉換稱為型別轉換。總結此函式的正確用法是將可迭代物件轉換為串列。
一個適當的例子是
C=串列(“字串”)
列印(C)
這將回傳 ['s', 't', 'r', 'i', 'n', 'g']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444483.html
標籤:Python python-3.x python-2.7
