简体   繁体   中英

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: list indices must be integers or slices, not str

I need you help please

I'm assuming your list is comprised of numbers and non-numbers. In Python, you can only access a list directly by providing integer indexes.

To avoid this issue, use the enumerate() function. This provides a list of tuples containing your value and an index you can use to access your list.

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)

Expected 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

etc.

EDIT:

If you're using a dictionary, you have to declare it using curly brackets:

>>> my_dict = {}

Then you can add fields to it like so:

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

and access it like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM