在python3.8中,有没有一种方法可以访问这些速率而不会出现类型错误:“dict\u keys”对象不可订阅?

2024-06-26 10:18:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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公司() 变量集(无)

你知道吗自选= tk.选项菜单(自行车架,可变,*比率,浮雕='凸起',bd=2,宽度=8,bg='#008085') 自选网格(行=2,列=2,padx=10,pady=10)


Tags: realtkunitedratesbrazilianchinadollaraustralian
1条回答
网友
1楼 · 发布于 2024-06-26 10:18:44

这是因为您试图访问Dictionary视图对象(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()循环),或者仅仅使用print(list(RATES.keys()))print(list(RATES.values()))将它转换成list

相关问题 更多 >