遍历键和MultiDict中的所有值 [英] Iterate over keys and all values in MultiDict
问题描述
我有字典
params = ImmutableMultiDict([('dataStore', 'tardis'), ('symbol', '1'), ('symbol', '2')])
我希望能够遍历字典并获得所有值及其键的列表.但是,当我尝试这样做时,它只会获取第一个符号键值对,而忽略另一个.
I want to be able to iterate through the dictionary and get a list of all the values and their keys. However, when I try to do it, it only gets the first symbol key value pair and ignores the other one.
for k in params:
print(params.get(k))
推荐答案
如果我对您的理解正确,那么您想遍历所有密钥,包括重复项,对吗?然后,您可以使用 items(multi = False)
方法,其中 multi
设置为 True
.
If I understand you correctly you want to iterate over all keys, including duplicates, right? Then you could use the items(multi=False)
method with multi
set to True
.
文档:
items( multi = False )
返回一个
(键,值)
对的迭代器.
参数: multi
–如果设置为 True ,则对于每个键的每个值,返回的迭代器将具有一对.否则它将仅包含配对以获取每个键的第一个值.
Parameters: multi
– If set to True the iterator returned will have a pair for each value of each key. Otherwise it will only contain
pairs for the first value of each key.
如果我误解了您,并且您想要一个按键的所有条目的列表,请查看jonrsharpe的答案.
If I misunderstood you and you want a list of all entries to a single key have a look at jonrsharpe's answer.
这篇关于遍历键和MultiDict中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!