I am trying to get my program to print out "banana"
from the dictionary. What would be the simplest way to do this?
我试图让我的程序从字典中打印出“banana”。最简单的方法是什么?
Please note that I am not trying to get my program to print out the value associated with "banana"
, but rather to print out "banana"
with a for loop, so when I run it, each key would also be printed.
请注意,我并不是试图让我的程序打印出与“banana”相关的值,而是用for循环打印出“banana”,所以当我运行它时,每个键也会打印出来。
This is my dictionary:
这是我的字典:
prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3
}
8 个解决方案
#1
66
If you only want one key of the dictionary, this is probably the shortest Python 2 and 3 valid code:
如果您只需要字典中的一个键,这可能是最短的Python 2和3有效代码:
my_dict = {'foo': 'bar'}
next(iter(my_dict)) # outputs 'foo'
#2
23
A dictionary is not indexed, but it is in some way, ordered. The following would give you the first existing key:
字典没有编入索引,但它在某种程度上是有序的。以下将为您提供第一个现有密钥:
list(my_dict.keys())[0]
#3
8
The dict
type is an unordered mapping, so there is no such thing as a "first" element.
dict类型是无序映射,因此没有“first”元素。
What you want is probably collections.OrderedDict
.
你想要的可能是collections.OrderedDict。
#4
7
If you want to print out all of the keys of the dict, iterate over it. You are not guaranteed the order here though, normal dicts will arrange themselves however they think will give the best performance.
如果要打印出dict的所有键,请迭代它。你不能保证订单在这里,正常的dicts会安排自己,但他们认为会给出最好的表现。
for k in prices:
print(k)
If you need to keep things in order, then you have to use an OrderedDict
and not just a plain dictionary.
如果你需要保持秩序,那么你必须使用OrderedDict,而不仅仅是一个普通的字典。
import collections
prices = collections.OrderedDict((
("banana", 4),
("apple", 2),
("orange", 1.5),
("pear", 3),
))
If you then wanted to see all the keys in order you could do so by iterating through it
如果你想要按顺序查看所有键,可以通过迭代来完成
for k in prices:
print(k)
You could, alternatively put all of the keys into a list and then work with that
您可以,或者将所有键放入列表中,然后使用它
ks = list(prices)
print(ks[0]) # will print "banana"
#5
1
d.keys()[0] to get the individual key.
d.keys()[0]获取个人密钥。
Update:- @AlejoBernardin , am not sure why you said it didn't work. here I checked and it worked. import collections
更新: - @AlejoBernardin,我不确定你为什么说它不起作用。在这里我检查了它的工作原理。导入集合
prices = collections.OrderedDict((
("banana", 4),
("apple", 2),
("orange", 1.5),
("pear", 3),
))
prices.keys()[0]
'banana'
'香蕉'
#6
0
As many others have pointed out there is no first value in a dictionary. The sorting in them is arbitrary and you can't count on the sorting being the same every time you access the dictionary. However if you wanted to print the keys there a couple of ways to it:
正如许多其他人所指出的那样,词典中没有第一个价值。它们中的排序是任意的,每次访问字典时都不能指望排序是相同的。但是,如果您想打印密钥,可以使用以下几种方法:
for key, value in prices.items():
print(key)
This method uses tuple assignment to access the key and the value. This handy if you need to access both the key and the value for some reason.
此方法使用元组分配来访问密钥和值。如果您出于某种原因需要访问密钥和值,这很方便。
for key in prices.keys():
print(key)
This will only gives access to the keys as the keys()
method implies.
这只会像keys()方法所暗示的那样访问密钥。
#7
0
Use a for loop that ranges through all keys in prices
:
使用for循环,其中包含价格中的所有键:
for key, value in prices.items():
print key
print "price: %s" %value
Make sure that you change prices.items()
to prices.iteritems()
if you're using Python 2.x
如果您使用的是Python 2.x,请确保将prices.items()更改为prices.iteritems()
#8
-2
This is one easy way to print banana
. Is this what you are looking for?
这是打印香蕉的一种简单方法。这是你想要的?
for key, value in prices.items() :
if value == 4:
print key
Of course this only works if there are no other items
in the dictionary with the value
4
. If there are other items
with the value
4
, you can do this:
当然,只有在字典中没有值为4的其他项目时,这才有效。如果还有其他值为4的项目,则可以执行以下操作:
for key, value in prices.items() :
if value == 4 and key.startswith("banan"):
print key
#1
66
If you only want one key of the dictionary, this is probably the shortest Python 2 and 3 valid code:
如果您只需要字典中的一个键,这可能是最短的Python 2和3有效代码:
my_dict = {'foo': 'bar'}
next(iter(my_dict)) # outputs 'foo'
#2
23
A dictionary is not indexed, but it is in some way, ordered. The following would give you the first existing key:
字典没有编入索引,但它在某种程度上是有序的。以下将为您提供第一个现有密钥:
list(my_dict.keys())[0]
#3
8
The dict
type is an unordered mapping, so there is no such thing as a "first" element.
dict类型是无序映射,因此没有“first”元素。
What you want is probably collections.OrderedDict
.
你想要的可能是collections.OrderedDict。
#4
7
If you want to print out all of the keys of the dict, iterate over it. You are not guaranteed the order here though, normal dicts will arrange themselves however they think will give the best performance.
如果要打印出dict的所有键,请迭代它。你不能保证订单在这里,正常的dicts会安排自己,但他们认为会给出最好的表现。
for k in prices:
print(k)
If you need to keep things in order, then you have to use an OrderedDict
and not just a plain dictionary.
如果你需要保持秩序,那么你必须使用OrderedDict,而不仅仅是一个普通的字典。
import collections
prices = collections.OrderedDict((
("banana", 4),
("apple", 2),
("orange", 1.5),
("pear", 3),
))
If you then wanted to see all the keys in order you could do so by iterating through it
如果你想要按顺序查看所有键,可以通过迭代来完成
for k in prices:
print(k)
You could, alternatively put all of the keys into a list and then work with that
您可以,或者将所有键放入列表中,然后使用它
ks = list(prices)
print(ks[0]) # will print "banana"
#5
1
d.keys()[0] to get the individual key.
d.keys()[0]获取个人密钥。
Update:- @AlejoBernardin , am not sure why you said it didn't work. here I checked and it worked. import collections
更新: - @AlejoBernardin,我不确定你为什么说它不起作用。在这里我检查了它的工作原理。导入集合
prices = collections.OrderedDict((
("banana", 4),
("apple", 2),
("orange", 1.5),
("pear", 3),
))
prices.keys()[0]
'banana'
'香蕉'
#6
0
As many others have pointed out there is no first value in a dictionary. The sorting in them is arbitrary and you can't count on the sorting being the same every time you access the dictionary. However if you wanted to print the keys there a couple of ways to it:
正如许多其他人所指出的那样,词典中没有第一个价值。它们中的排序是任意的,每次访问字典时都不能指望排序是相同的。但是,如果您想打印密钥,可以使用以下几种方法:
for key, value in prices.items():
print(key)
This method uses tuple assignment to access the key and the value. This handy if you need to access both the key and the value for some reason.
此方法使用元组分配来访问密钥和值。如果您出于某种原因需要访问密钥和值,这很方便。
for key in prices.keys():
print(key)
This will only gives access to the keys as the keys()
method implies.
这只会像keys()方法所暗示的那样访问密钥。
#7
0
Use a for loop that ranges through all keys in prices
:
使用for循环,其中包含价格中的所有键:
for key, value in prices.items():
print key
print "price: %s" %value
Make sure that you change prices.items()
to prices.iteritems()
if you're using Python 2.x
如果您使用的是Python 2.x,请确保将prices.items()更改为prices.iteritems()
#8
-2
This is one easy way to print banana
. Is this what you are looking for?
这是打印香蕉的一种简单方法。这是你想要的?
for key, value in prices.items() :
if value == 4:
print key
Of course this only works if there are no other items
in the dictionary with the value
4
. If there are other items
with the value
4
, you can do this:
当然,只有在字典中没有值为4的其他项目时,这才有效。如果还有其他值为4的项目,则可以执行以下操作:
for key, value in prices.items() :
if value == 4 and key.startswith("banan"):
print key