i want convert string to dictionary for example
my string is "Man I Je Ich"
so dictionary result is
{
'I' : 'Man',
'Je': 'Man',
'Ich': 'Man'
}
first word is value and 3 more words is keys
CodePudding user response:
You could get the first word and the rest in separate variables then use a dict
comprehension to create the dict
:
s = "Man I Je Ich"
val, *keys = s.split()
data = {k: val for k in keys}
{'I': 'Man', 'Je': 'Man', 'Ich': 'Man'}
CodePudding user response:
Try this dud..
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)
CodePudding user response:
this works file:
string = "Man I Je Ich"
keys = string.split()
data = {key: keys[0] for key in keys[1:]}