如何一次为dict对象中的多个键分配相同的值?

时间:2022-02-10 20:37:05

Here is my question:

这是我的问题:

k = "_opqrst_ _ _ _ yzabc_ _ _ _hijklm"
dict_ = dict.fromkeys(zip(string.ascii_lowercase , k))

the result is:

结果是:

{'d': 'q', 'e': 'r', 'x': 'k', 'i': '_', 'o': 'b', 'b': 'o', 'm': 'z', 'k': '_',
'j': '_', 'u': 'h', 'n': 'a', 's': '_', 'a': '_', 'w': 'j', 'v': 'i', 'c': 'p',
't': '_', 'z': 'm', 'f': 's', 'l': 'y', 'p': 'c', 'g': 't', 'h': '_', 'y': 'l',
'r': '_', 'q': '_'} 

I want my programme showing like this

我希望我的程序显示如下

{'D,d': 'q', 'E,e': 'r', 'X,x': 'k', 'I,i': '_', 'O,o': 'b', 'B,b': 'o', ......}

That means {'capital, small alphabet': ' '}

这意味着{'资本,小字母':''}

2 个解决方案

#1


0  

{'D,d': 'q'} is still mapping a single key to a value. To map multiple keys to the same value would be something like

{'D,d':'q'}仍然将单个键映射到值。将多个键映射到相同的值将是类似的

from itertools import cycle
from string import ascii_letters

k = '_opqrst____yzabc____hijklm'
d = dict(zip(ascii_letters, cycle(k)))

d is then

那么就是

{'V': 'i', 'j': '_', 'E': 'r', 'B': 'o', 'S': '_', 'e': 'r', 'G': 't',
'l': 'y', 'O': 'b', 'k': '_', 'Y': 'l', 'o': 'b', 'R': '_', 't': '_', 'g': 
't', 'v': 'i', 'A': '_', 'C': 'p', 'u': 'h', 'J': '_', 'N': 'a', 'I': '_', 
'w': 'j', 'b': 'o', 'z': 'm', 'x': 'k', 'q': '_', 'a': '_', 'm': 'z', 'X': 
'k', 'i': '_', 'h': '_', 'p': 'c', 'W': 'j', 'r': '_', 'd': 'q', 'U': 'h', 
'P': 'c', 'F': 's', 'c': 'p', 'f': 's', 'y': 'l', 'D': 'q', 'Z': 'm', 's': 
'_', 'n': 'a', 'H': '_', 'Q': '_', 'M': 'z', 'L': 'y', 'T': '_', 'K': '_'}

If you want the 'D,d' keys, you can do

如果你想要'D,d'键,你可以做到

from string import ascii_lowercase

d = {'{},{}'.format(a.upper(), a): b for a, b in zip(ascii_lowercase, k)}

#2


0  

You will need to construct your keys, then build the dictionary:

您需要构建密钥,然后构建字典:

import string
k = "_opqrst_ _ _ _ yzabc_ _ _ _hijklm"
k_wthout_whites = ''.join([c for c in k if c != ' '])   # removing white space
keys = [','.join(elt) for elt in (zip(string.ascii_uppercase, string.ascii_lowercase))]
dict_ = {key: val for key, val in zip(keys, k_wthout_whites)}
dict_

output:

{'A,a': '_',
 'B,b': 'o',
 'C,c': 'p',
 'D,d': 'q',
 'E,e': 'r',
 'F,f': 's',
 'G,g': 't',
 'H,h': '_',
 'I,i': '_',
 'J,j': '_',
 'K,k': '_',
 'L,l': 'y',
 'M,m': 'z',
 'N,n': 'a',
 'O,o': 'b',
 'P,p': 'c',
 'Q,q': '_',
 'R,r': '_',
 'S,s': '_',
 'T,t': '_',
 'U,u': 'h',
 'V,v': 'i',
 'W,w': 'j',
 'X,x': 'k',
 'Y,y': 'l',
 'Z,z': 'm'}

#1


0  

{'D,d': 'q'} is still mapping a single key to a value. To map multiple keys to the same value would be something like

{'D,d':'q'}仍然将单个键映射到值。将多个键映射到相同的值将是类似的

from itertools import cycle
from string import ascii_letters

k = '_opqrst____yzabc____hijklm'
d = dict(zip(ascii_letters, cycle(k)))

d is then

那么就是

{'V': 'i', 'j': '_', 'E': 'r', 'B': 'o', 'S': '_', 'e': 'r', 'G': 't',
'l': 'y', 'O': 'b', 'k': '_', 'Y': 'l', 'o': 'b', 'R': '_', 't': '_', 'g': 
't', 'v': 'i', 'A': '_', 'C': 'p', 'u': 'h', 'J': '_', 'N': 'a', 'I': '_', 
'w': 'j', 'b': 'o', 'z': 'm', 'x': 'k', 'q': '_', 'a': '_', 'm': 'z', 'X': 
'k', 'i': '_', 'h': '_', 'p': 'c', 'W': 'j', 'r': '_', 'd': 'q', 'U': 'h', 
'P': 'c', 'F': 's', 'c': 'p', 'f': 's', 'y': 'l', 'D': 'q', 'Z': 'm', 's': 
'_', 'n': 'a', 'H': '_', 'Q': '_', 'M': 'z', 'L': 'y', 'T': '_', 'K': '_'}

If you want the 'D,d' keys, you can do

如果你想要'D,d'键,你可以做到

from string import ascii_lowercase

d = {'{},{}'.format(a.upper(), a): b for a, b in zip(ascii_lowercase, k)}

#2


0  

You will need to construct your keys, then build the dictionary:

您需要构建密钥,然后构建字典:

import string
k = "_opqrst_ _ _ _ yzabc_ _ _ _hijklm"
k_wthout_whites = ''.join([c for c in k if c != ' '])   # removing white space
keys = [','.join(elt) for elt in (zip(string.ascii_uppercase, string.ascii_lowercase))]
dict_ = {key: val for key, val in zip(keys, k_wthout_whites)}
dict_

output:

{'A,a': '_',
 'B,b': 'o',
 'C,c': 'p',
 'D,d': 'q',
 'E,e': 'r',
 'F,f': 's',
 'G,g': 't',
 'H,h': '_',
 'I,i': '_',
 'J,j': '_',
 'K,k': '_',
 'L,l': 'y',
 'M,m': 'z',
 'N,n': 'a',
 'O,o': 'b',
 'P,p': 'c',
 'Q,q': '_',
 'R,r': '_',
 'S,s': '_',
 'T,t': '_',
 'U,u': 'h',
 'V,v': 'i',
 'W,w': 'j',
 'X,x': 'k',
 'Y,y': 'l',
 'Z,z': 'm'}