相关疑难解决方法(0)

Python反向/反转映射

给出这样的字典:

my_map = {'a': 1, 'b': 2}
Run Code Online (Sandbox Code Playgroud)

如何反转此地图以获得:

inv_map = {1: 'a', 2: 'b'}
Run Code Online (Sandbox Code Playgroud)

编者注: __CODE__改为__CODE__避免与内置函数冲突,__CODE__.下面有些评论可能会受到影响.

python mapping reverse dictionary

597
推荐指数
12
解决办法
36万
查看次数

Python3字典迭代值?

嘿大家我正在尝试用Python编写一个程序来充当测验游戏.我在程序开头创建了一个字典,其中包含用户将被测验的值.它的设置如下:

PIX0 = {"QVGA":"320x240", "VGA":"640x480", "SVGA":"800x600"}
Run Code Online (Sandbox Code Playgroud)

所以我定义了一个函数,它使用for循环遍历字典键并要求用户输入,并将用户输入与匹配的值进行比较.

for key in PIX0:
    NUM = input("What is the Resolution of %s?"  % key)
    if NUM == PIX0[key]:
        print ("Nice Job!")
        count = count + 1
    else:
        print("I'm sorry but thats wrong. The correct answer was: %s." % PIX0[key] )
Run Code Online (Sandbox Code Playgroud)

这是工作正常输出看起来像这样:

What is the Resolution of Full HD? 1920x1080
Nice Job!
What is the Resolution of VGA? 640x480
Nice Job!
Run Code Online (Sandbox Code Playgroud)

所以我希望能够做的是有一个单独的功能,以另一种方式询问问题,为用户提供分辨率编号并让用户输入显示标准的名称.所以我想创建一个for循环,但我真的不知道如何(或者你是否可以)迭代字典中的值并要求用户输入密钥.

我想要输出看起来像这样:

Which standard has a resolution of 1920x1080? Full HD …
Run Code Online (Sandbox Code Playgroud)

python dictionary loops key-value

40
推荐指数
1
解决办法
8万
查看次数

Python:获取字典索引的关键字

可能重复:
反向字典查找 - 使用Python 进行字典的Python
反向映射

如何在字典中获取索引的键?

例如:

i = {'a': 0, 'b': 1, 'c': 2}
Run Code Online (Sandbox Code Playgroud)

所以,如果我想获得i [0]的关键,它将返回'a'

python dictionary

18
推荐指数
3
解决办法
12万
查看次数

使用Python反向映射字典

可能重复:
反向字典查找 - Python

如果我有一个名为ref的字典如下

ref = {}
ref["abc"] = "def"
Run Code Online (Sandbox Code Playgroud)

我可以从"abc"获得"def"

def mapper(from):
    return ref[from]
Run Code Online (Sandbox Code Playgroud)

但是,我如何才能从"def"变为"abc"?

def revmapper(to):
    ??????
Run Code Online (Sandbox Code Playgroud)

python dictionary

17
推荐指数
2
解决办法
1万
查看次数

按字典中的值返回键

我试图在给定值的字典中返回密钥

在这种情况下,如果'b'在字典中,我希望它返回'b'所在的键(即2)

def find_key(input_dict, value):
    if value in input_dict.values():
        return UNKNOWN            #This is a placeholder
    else:
        return "None"

print(find_key({1:'a', 2:'b', 3:'c', 4:'d'}, 'b'))
Run Code Online (Sandbox Code Playgroud)

我想得到的答案是关键2,但我不确定要放什么以获得答案,任何帮助将不胜感激

python dictionary key python-3.x

3
推荐指数
2
解决办法
2万
查看次数

通过Python的dict中的值查找键

NAMES  =  ['Alice', 'Bob','Cathy','Dan','Ed','Frank',
           'Gary','Helen','Irene','Jack', 'Kelly','Larry'] 
AGES  =  [20,21,18,18,19,20,20,19,19,19,22,19]

def nameage(a,b):
    nameagelist = [x for x in zip(a,b)]
    nameagedict = dict(nameagelist)

    return nameagedict

def name(a):
    for x in nameage(NAMES,AGES):
        if a in nameage(NAMES,AGES).values():
            print nameage(NAMES,AGES).keys()

print name(19)
Run Code Online (Sandbox Code Playgroud)

我试图返回19岁的人的姓名.如何按价值搜索字典并返回密钥?

python dictionary

2
推荐指数
1
解决办法
6423
查看次数

索引python字典的值

可能重复:
反向字典查找 - Python

是否有内置的方法在Python中按值索引字典.

例如:

dict = {'fruit':'apple','colour':'blue','meat':'beef'}
print key where dict[key] == 'apple'
Run Code Online (Sandbox Code Playgroud)

要么:

dict = {'fruit':['apple', 'banana'], 'colour':'blue'}
print key where 'apple' in dict[key]
Run Code Online (Sandbox Code Playgroud)

或者我必须手动循环吗?

python dictionary

1
推荐指数
1
解决办法
4940
查看次数

逆字典映射的性能

获取所有dict项目value == 3并创建新dict 的最有效方法是什么?

这是我到目前为止:

d = {1: 2, 2: 2, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, ...}
new_d = {}
for item in d:
    if d[item] == 3:
        new_d[item] = d[item]
Run Code Online (Sandbox Code Playgroud)

有没有更有效,更简单的方法来做到这一点?也许使用地图?

python dictionary

-1
推荐指数
1
解决办法
56
查看次数

标签 统计

dictionary ×8

python ×8

key ×1

key-value ×1

loops ×1

mapping ×1

python-3.x ×1

reverse ×1