在列表中操作词典

2024-06-16 15:17:43 发布

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

我有一个包含字典的列表,如下所示:

wordsList = [
    {'Definition': 'Allows you to store data with ease' , 'Word': 'Database'},
    {'Definition': 'This can either be static or dynamic' , 'Word': 'IP'},
]

基本上,我想做的是:

  • 能够打印每个单独的定义
  • 能够打印每个单独的单词

所以我的问题是:我该怎么做?我只知道如何使用常规列表/字典,而不知道我这里有什么。你知道吗


Tags: tostoreyou列表data字典withthis
2条回答
for word_def in wordsList:
  print word_def.get("Word")
  print word_def.get("Definition")
  print

输出

Database
Allows you to store data with ease

IP
This can either be static or dynamic

基本上,这些是“常规”列表/字典。你知道吗

您必须了解,Python中的列表可以包含任何对象,也可以包含dict。因此,无论是列表还是包含的内容都不会变得“不规则”。你知道吗

您可以访问列表/目录中的任何内容,例如:

word_def[index][name]

具有适当的索引/名称值。你知道吗

您还可以遍历列表(如SSNR所示),从而获取包含的任何词典,并像处理普通dict一样处理它们。你知道吗

你也可以通过这种方式掌握其中一条格言:

one_dict = word_def[index]

不仅仅是访问内容:

value = one_dict[name]

相关问题 更多 >