簡體   English   中英

列表索引必須是整數或切片而不是 str python

[英]list indices must be integers or slices not str python

@classmethod
def RoundToValidQuantity(cls, symbol_data, desired_quantity, round_up: bool = False) -> Decimal:
    """ Returns the minimum quantity of a symbol we can buy,
    closest to desiredPrice """

    lot_filter = {}

    for fil in symbol_data["filters"]:
        if fil["filterType"] == "LOT_SIZE":
            lot_filter = fil
            break

TypeError:列表索引必須是整數或切片,而不是 str

我需要你幫忙

我假設您的列表由數字和非數字組成。 在 Python 中,您只能通過提供 integer 索引來直接訪問列表。

為避免此問題,請使用enumerate() function。 這提供了一個包含您的值的元組列表和一個可用於訪問您的列表的索引。

list = [1, 2, "a", "b", 3, 4, "c", "d"]

for index, value in enumerate(list):
  print("the index is: " + index)
  print("the value is: " + value)

預期 output:

the index is: 0
the value is: 1

the index is: 1
the value is: 2

the index is: 2
the value is: a

the index is: 3
the value is b

等等

編輯:

如果您使用的是字典,則必須使用大括號聲明它:

>>> my_dict = {}

然后你可以像這樣向它添加字段:

>>> my_dict["field_name"] = "blah"

並像這樣訪問它:

>>> my_dict["field_name"]
'blah'

暫無
暫無

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

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