d = {'ANIMAL' : ['CAT','DOG','FISH','HEDGEHOG']}
for item in d:
for animal in (d[item]):
if animal == "DOG":
print(animal)
Update -What if I want to compare the string to see if they're equal or not... let say if the value at the first index is equal to the value at the second index.
您可以使用:
d = {'ANIMAL' : ['CAT','DOG','FISH','HEDGEHOG']}
for item in d:
for animal in (d[item]):
if animal == "DOG":
if list(d.keys())[0] == list(d.keys())[1]:
print("Equal")
else: print("Unequal")
d = {'ANIMAL' : ['CAT','DOG','FISH','HEDGEHOG']}
for keys, values in d.items(): #Will allow you to reference the key and value pair
for item in values: #Will iterate through the list containing the animals
if item == "DOG":
print(item)
print(values.index(item)) #will tell you the index of "DOG" in the list.
所以也许这会有帮助:
您可以使用:
您可以执行以下操作(包括注释):
字典中的键和值是按键索引的,不像列表中那样有固定的索引。你知道吗
但是,您可以利用“OrderedDict”为词典提供索引方案。它很少使用,但很方便。你知道吗
也就是说,python3.6中的词典是按插入顺序排列的:
更多信息请参见:
Are dictionaries ordered in Python 3.6+?
相关问题 更多 >
编程相关推荐