我想將字串轉換成字典,例如
我的字串是"Man I Je Ich"。
所以字典的結果是
{
'I'/span> : 'Man'/span>,
'Je': 'Man',
'Ich': 'Man'.
第一個詞是值,還有3個詞是鍵
。uj5u.com熱心網友回復:
你可以在不同的變數中獲得第一個詞和其余的詞,然后使用dict理解力來創建dict:
s = "Man I Je Ich"
val, *keys = s.split()
data = {k: val for k in keys}.
{'I'/span>: 'Man'/span>, 'Je'/span>: 'Man'/span>, 'Ich'/span>: 'Man'}。
uj5u.com熱心網友回復:
試試這個啞巴。
string = "Man I Je Ich"。
#split string to words.
words = string.split()
#get first word as key;/span>
key = words[0]
#remove the key from words 移除關鍵詞。
del words[0]
#創建你的字典
dictionary = {}
for word in words:
dictionary[word] = key
print(dictionary)
uj5u.com熱心網友回復:
這個作業檔案:
string = "Man I Je Ich"
keys = string.split()
data = {key: keys[0] for key in keys[1: ] }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311387.html
標籤:
