"Man I Je Ich"
所以字典结果是
{
'I' : 'Man',
'Je': 'Man',
'Ich': 'Man'
}
第一个单词是值,另外 3 个单词是键
dict
理解来创建
dict
:
s = "Man I Je Ich"
val, *keys = s.split()
data = {k: val for k in keys}
# or
data = dict.fromkeys(keys, val)
>>> data
{'I': 'Man', 'Je': 'Man', 'Ich': 'Man'}
string = "Man I Je Ich"
#split string to words
words = string.split()
#get first word as key
key = words[0]
#remove the key from words
del words[0]
#create your dictionary
dictionary = {}
for word in words:
dictionary[word] = key
print(dictionary)
string = "Man I Je Ich"
keys = string.split()
data = {key: keys[0] for key in keys[1:]}