簡體   English   中英

有沒有一種方法可以在 python 3.8 中訪問這些速率而不會出現 TypeError: 'dict_keys' object is not subscriptable?

[英]Is there a way i can access these Rates in python 3.8 without getting a TypeError: 'dict_keys' object is not subscriptable?

RATES = {
        "Australian Dollar":1.4099,
        "Brazilian Real":3.7927,
        "Canadian dollar":1.3375,
        "Switzerland Franc":0.9964,
        "China Yuan":6.7131,
        "Euro":0.8845,
        "United Kingdom Pound":0.763,
        "Hungarian Forint":279.337,
        "Indian Rupees":68.98,
        "Japanese Yen":110.5194,
        "Kenyan shilling":100.6989,
        "Korean Won":1133.5973,
        "Malawian Kwacha":723.985,
        "New Zealand dollar":1.4558,
        "Oman Riyal":0.385,
        "Tanzanian Shilling":2344.103,
        "Ugandan Shilling":3708.5025,
        "United States Dollar":1,
        "South African Rand":14.3397,
        "Zambian Kwacha":12.029
        }

變量 = tk.StringVar() variable.set(None)

self.opt = tk.OptionMenu(self.frame, variable, *RATES, Relief = 'raised', bd =2, width = 8, bg = '#008085') self.opt.grid(row = 2, column = 2、padx = 10,pady = 10)

那是因為您正在嘗試訪問字典視圖對象( https://docs.python.org/3.8/library/stdtypes.html#dictionary-view-objects

來自 Python 文檔網站的一些示例

>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()

>>> # iteration
>>> n = 0
>>> for val in values:
...     n += val
>>> print(n)
504

>>> # keys and values are iterated over in the same order (insertion order)
>>> list(keys)
['eggs', 'sausage', 'bacon', 'spam']
>>> list(values)
[2, 1, 1, 500]

>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['bacon', 'spam']

>>> # set operations
>>> keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
>>> keys ^ {'sausage', 'juice'}
{'juice', 'sausage', 'bacon', 'spam'}

所以基本上你可以迭代它( for key in RATES.keys()循環中使用for key in RATES.keys() ),或者只是使用print(list(RATES.keys()))print(list(RATES.values()))將其轉換為列表

暫無
暫無

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

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