简体   繁体   English

如何在Python中打印字典的键?

[英]How do you print a key of a dictionary in Python?

Suppose I have a dictionary: 假设我有一本字典:

dictionary1 = {'test':'output','test2':'output2'}

How would I be able to print the key, test , on the screen? 如何在屏幕上打印test键?

I only want one of the keys at a time, not all of them. 我一次只想要其中一个键, 而不是全部。

By the way, not literally, by doing print('test') , I mean how do you print the key of any dictionary? 顺便说一句,不是字面上地,通过执行print('test') ,我的意思是你如何打印任何字典的键?

Like is there something like this: 就像有这样的事情:

#pseudocode
x = dictionary1.keys()[0]
>>> print(x)
'test'

I do not want to sort the dictionary that I'm actually using in my program. 我不想对我在程序中实际使用的字典进行排序。

A dictionary may have any number of keys, 0+. 字典可以具有任意数量的键,即0+。 To print them all (if any) in sorted order, you could 要按排序顺序打印所有(如果有),您可以

for key in sorted(dictionary1):
    print(key)

If you don't care about the order (ie you're fine with a random-looking order), remove the sorted call. 如果您不关心订单(即您对外观随机的订单还可以),请删除已sorted通话。

If you want to be selective (eg only print keys that are sequences of length 4, so that in your example test would be printed but test2 wouldn't): 如果您希望选择(例如,仅打印长度为4的序列的打印键,那么在您的示例中将打印test ,但不会打印test2 ):

for key in sorted(dictionary1):
    try:
        if len(key) == 4:
            print(key)
    except TypeError:
        pass

If you want to print only one such key, add a break after the print . 如果您只想打印一个这样的键,请在print后添加一个break

There are at least 347 other things you could mean by your extremely vague question, but I tried to field the ones that seem most likely... if that's not enough for you, edit your question to make it much more precise!-) 还有其他至少347分的东西,你可以用你的极其模糊的问题的意思,但我试图到现场,似乎最有可能的......如果这还不够你的那些,编辑你的问题,使之更加精确- !)

Added: the edit only said the OP wants to print only one key and do no sorting, so it remains a mystery how that one key is to be picked (I suspect there's a misunderstanding -- as if dict keys had a specific order, which of course in Python they don't, and the OP wants "the first one", which just can't be pinned down -- OrderedDict is a very different [and alas inevitably slower] beast, but dict s what the OP's showing). 补充:编辑只说OP只想打印一个键并且不进行排序,因此如何选择一个键仍然是个谜(我怀疑这是一个误解-就像dict键有特定顺序,在Python当然他们不这样做,和OP要“第一个”,这只是不能牵制- OrderedDict是一个非常不同的[唉必然较慢]兽,但dict有什么OP的表现) 。

So if the goal is to print "any one key, no matter which one" (and it is known that thedict is not empty): 因此,如果目标是打印“任何一个键,无论是哪个键”(并且知道该thedict不为空):

print(next(iter(thedict)))

is the simplest, most direct way -- a dict is iterable, yielding its keys in arbitrary order; 是最简单,最直接的方法- dict是可迭代的,以任意顺序产生其键; iter returns an iterator on its iterable argument; iter在其可迭代参数上返回一个迭代器; and next returns the first (first in arbitrary order means an arbitrary one of course) item of its iterator argument. 然后next返回其迭代器参数的第一项(以任意顺序表示自然是任意项)。

If it's possible for thedict to be empty, and nothing must be printed in that case, just add a guard: 如果thedict可能为空,并且在这种情况下任何内容都不能打印,则只需添加一个防护即可:

if thedict: print(next(iter(thedict)))

This will print nothing for an empty dictionary, the only key for a dictionary with length 1, an arbitrary key for a dictionary with length greater than 1. 对于空字典,这将不打印任何内容;对于长度为1的字典,此键为唯一键;对于长度大于1的字典,此键为任意键。

If you're trying to find a key associated with a specific value you can do 如果您要查找与特定值关联的键,则可以执行

keys = [key for key in d if d[key] == value]
print(keys)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM