【问题标题】:How to specifically set a value in a dictionary as an element on python?如何专门将字典中的值设置为python上的元素?
【发布时间】:2021-05-22 18:58:36
【问题描述】:

我正在尝试找到一种方法来了解如何在字典中选择一个值并将其设置为一个元素。

例如:

Countries = {'Africa':['Morocco', 'Algeria', 'Egypt'], 'Europe':['Uk', 'France', 'Germany']}

我该如何设置,

a = Germany

不知道德国在字典里吗?

我尝试将“a”设置为列表,然后尝试将部分国家/地区附加到“a”中。

a = Countries['Europe' : -1]

但是这会出现 'TypeError unhashable type: 'slice'

任何帮助都会很可爱。

【问题讨论】:

  • a = Countries['Europe'][-1]。这会将a 设置为欧洲列表中的最后一项(无论它是什么)。

标签: python arrays list dictionary


【解决方案1】:

解决办法是:

a = Countries['Europe'][-1]

这可以访问所有欧洲国家:

europe = Countries['Europe']

这是最后一个欧洲国家:

last_europe = europe[-1]

如此组合:

a = Countries['Europe'][-1]

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 2012-07-13
  • 2012-08-16
  • 1970-01-01
  • 2021-09-21
  • 2010-12-17
  • 2018-01-06
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多