如何在Python中直接获取字典键作为变量(而不是从值中搜索)?

如何在Python中直接获取字典键作为变量(而不是从值中搜索)?,python,dictionary,key,Python,Dictionary,Key,对于这个基本的问题,我感到抱歉,但是我在这方面的搜索没有找到任何结果,除了如何根据字典的值获取字典的键,我不想使用它,因为我只是想要键的文本/名称,并且担心如果字典有很多条目,按值搜索可能会返回2个或更多键。。。我想做的是: mydictionary={'keyname':'somevalue'} for current in mydictionary: result = mydictionary.(some_function_to_get_key_name)[current] p

对于这个基本的问题,我感到抱歉,但是我在这方面的搜索没有找到任何结果,除了如何根据字典的值获取字典的键,我不想使用它,因为我只是想要键的文本/名称,并且担心如果字典有很多条目,按值搜索可能会返回2个或更多键。。。我想做的是:

mydictionary={'keyname':'somevalue'}
for current in mydictionary:

   result = mydictionary.(some_function_to_get_key_name)[current]
   print result
   "keyname"
这样做的原因是,我正在将这些内容打印到文档中,我希望在执行此操作时使用密钥名和值

我看到了下面的方法,但这似乎只是返回键的值

get(key[, default])

您应该使用以下命令迭代键:

for key in mydictionary:
   print "key: %s , value: %s" % (key, mydictionary[key])

如果要同时访问键和值,请使用以下命令:

Python 2:

for key, value in my_dict.iteritems():
    print(key, value)
for key, value in my_dict.items():
    print(key, value)
Python 3:

for key, value in my_dict.iteritems():
    print(key, value)
for key, value in my_dict.items():
    print(key, value)
这样做的原因是,我正在将这些内容打印到文档中,我希望在执行此操作时使用密钥名和值

基于上述要求,我建议:

keys = mydictionary.keys()
keys.sort()

for each in keys:
    print "%s: %s" % (each, mydictionary.get(each))

keys=[i for i in mydictionary.keys()]
keys=list(mydictionary.keys())
迭代字典(i)将返回键,然后使用它(i)获取值

for i in D:
    print "key: %s, value: %s" % (i, D[i])

如果字典中包含一对类似的内容:

d = {'age':24}
然后你就可以得到

field, value = d.items()[0]
对于Python 3.5,请执行以下操作:

key = list(d.keys())[0]
对于python 3 如果你只想得到钥匙,就用这个。如果需要打印(值),请将打印(键)替换为打印(值)

for key,value in my_dict:
  print(key)
就这么简单:

mydictionary={'keyname':'somevalue'}
result = mydictionary.popitem()[0]

您将修改您的字典,并应首先复制它

我有时会创建另一个字典,以便能够以字符串形式访问我认为需要的任何内容。然后,我迭代多个匹配键的字典,以构建一个以第一列作为描述的表

dict_names = {'key1': 'Text 1', 'key2': 'Text 2'}
dict_values = {'key1': 0, 'key2': 1} 

for key, value in dict_names.items():
    print('{0} {1}'.format(dict_names[key], dict_values[key])
您可以轻松地使用大量字典来匹配数据(我喜欢这样的事实,即使用字典,您可以始终引用一些众所周知的关键字名称)

是的,我使用字典来存储函数的结果,所以我不需要每次调用这些函数一次就运行它们,然后随时访问结果


编辑:在我的示例中,键名实际上并不重要(我个人喜欢使用相同的键名,因为从任何匹配的字典中选择单个值更容易),只要确保每个字典中的键数相同即可

您只需使用
*
解压字典键即可。例如:

d = {'x': 1, 'y': 2}
t = (*d,)
print(t) # ('x', 'y')

轻松更改关键点和值的位置,然后使用值获取关键点, 字典中的键可以具有相同的值,但它们(键)应该不同。 例如,如果您有一个列表,它的第一个值是问题的关键,而其他值是第一个值的规格:

list1=["Name",'ID=13736','Phone:1313','Dep:pyhton']
通过此循环,您可以在字典中轻松保存和使用数据:

data_dict={}
for i in range(1, len(list1)):
     data_dict[list1[i]]=list1[0]
print(data_dict)
{'ID=13736': 'Name', 'Phone:1313': 'Name', 'Dep:pyhton': 'Name'}

然后,您可以根据任何输入值查找键(名称)。

您可以通过将dict键和值强制转换为列表来完成此操作。也可以对项目执行此操作

例如:

f = {'one': 'police', 'two': 'oranges', 'three': 'car'}
list(f.keys())[0] = 'one'
list(f.keys())[1] = 'two'

list(f.values())[0] = 'police'
list(f.values())[1] = 'oranges'

下面的代码是创建地图到经理玩家点。目标是将单词“player”与序列号连接起来

players_numbers = int(input('How many girls will play? ')) #First - input receive a input about how many people will play

players = {}  
counter = 1

for _ in range(players_numbers): #sum one, for the loop reach the correct number
    player_dict = {f'player{counter}': 0} #concatenate the word player with the player number. the initial point is 0.
    players.update(player_dict) #update the dictionary with every player
    counter = counter + 1
    print(players)

输出>>
{'player1':0,'player2':0,'player3':0}…

如果您只需要从简单的字典中获取键值,例如:

os_type = {'ubuntu': '20.04'}
使用
popitem()
方法:

os, version = os_type.popitem()
print(os) # 'ubuntu'
print(version) # '20.04'

您是否只想检查字典中是否存在“keyname”?因为你已经有了。不,正如我所说的,需要打印出来,它将通过大量的键进行迭代
current
是当前键,只需执行
print current
你如何获得字典中的第一个键?(无迭代)请确保使用代码标记并解释您的答案。您可以在python 3.6中为mydictionary中的键编写
:打印(f“key:{key},value:{mydictionary[key]}”)Python3.5:
类型错误:“dict\u items”对象不支持索引
非常粗糙,但d.\uu iter\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu做一个简单的动作。你有“年龄”。在Python3中,这适用于一对dict:
key=list(d.keys())[0]
如果你有一个巨大的dict,使用
next()
更好:我更喜欢其他答案,比如
my dict.item()
或者像
keys=list这样的列表理解(my_dict.keys)
但是为了防止有人想在Python 3中这样做:(第2行)
keys=sorted(my_dict.keys())
+添加()打印。对于Python 3:items()而不是iteritems()python34:SyntaxError:只能将带星号的表达式用作赋值目标Dang,我使用python34是因为我无法在windows测试环境中运行任何更新的内容。