Python 如何从字典中打印特定的键值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48894060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print Specific key value from a dictionary?
提问by Irakli
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key,value in fruit.items():
print(value)
I want to print the kiwi key, how?
我想打印奇异果钥匙,怎么办?
print(value[2])
This is not working.
这是行不通的。
回答by Joe Iddon
Python's dictionaries have no order, so indexing like you are suggesting (fruits[2]
) makes no sense as you can't retrieve the second element of something that has no order. They are merely sets of key:value
pairs.
Python 的字典没有 order,因此像您建议的 ( fruits[2]
)索引没有任何意义,因为您无法检索没有顺序的内容的第二个元素。它们只是key:value
成对的集合。
To retrieve the value at key
: 'kiwi'
, simply do: fruit['kiwi']
. This is the most fundamental way to access the value of a certain key. See the documentationfor further clarification.
要检索的值key
:'kiwi'
,简单地做:fruit['kiwi']
。这是访问某个键值的最基本方式。请参阅文档以获取进一步说明。
And passing that into a print()
call would actually give you an output:
并将其传递给print()
调用实际上会给你一个输出:
print(fruit['kiwi'])
#2.0
Note how the 2.00
is reduced to 2.0
, this is because superfluous zeroes are removed.
注意 是如何2.00
减少到 的2.0
,这是因为多余的零被删除了。
Finally, if you want to use a for-loop
(don't know why you would, they are significantly more inefficient in this case (O(n)
vs O(1)
for straight lookup)) then you can do the following:
最后,如果您想使用 a for-loop
(不知道为什么会这样,在这种情况下它们的效率要低得多(O(n)
与O(1)
直接查找相比)),那么您可以执行以下操作:
for k, v in fruit.items():
if k == 'kiwi':
print(v)
#2.0
回答by arundeepak
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key,value in fruit.items():
if value == 2.00:
print(key)
I think you are looking for something like this.
我想你正在寻找这样的东西。
回答by Alan Hoover
You can access the value of key 'kiwi' with
您可以访问密钥 'kiwi' 的值
print(fruit['kiwi'])
回答by MOHANPAL SINGH
You can simply print by using below command
您可以使用以下命令简单地打印
print(fruit['kiwi'])
If you want to check whether a key has been successfully added or not, use below command:
如果要检查密钥是否已成功添加,请使用以下命令:
print('key' in dictionary)
for eg:
print('New york' in USA)
例如:
print('New york' in USA)
回答by Abhisek Roy
Is this what you are looking for?
这是你想要的?
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key in fruit:
print(fruit[key],"=",key)
回答by Abhisek Roy
If you only want to display the Kiwi field.
如果您只想显示 Kiwi 字段。
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
print(fruit["kiwi"],"=kiwi")
回答by ColonelFazackerley
def reverse_dict(dictionary, lookup_val):
for key,value in fruit.items():
if abs(lookup_val-value) < 0.01:
return key
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key,value in fruit.items():
print("{}:{}".format(key, value))
print(fruit['kiwi'])
print(reverse_dict(fruit, 2.00))
Many reasons for caution
谨慎的原因很多
- This will get slow with a large dict
- only first match will be returned
- your value is a float, so a tolerance is needed: I picked 0.01
- 这会因大字典而变慢
- 只会返回第一场比赛
- 你的值是一个浮点数,所以需要一个公差:我选择了 0.01