Skip to content

Commit d44f5a6

Browse files
author
Amogh Singhal
authored
Create processStringToDict.py
1 parent ddf0d49 commit d44f5a6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

processStringToDict.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Process the string "k:1 |k1:2|k2:3|k3:4" into a dictionary {k:1,k1:2,...}
2+
3+
StringToProcess = "k:1 |k1:2|k2:3|k3:4"
4+
5+
d2 = dict()
6+
keyvalue_list = StringToProcess.split('|') # ['k:1' , 'k1:2' , 'k2:3' ,'k3:4']
7+
8+
for keyval in keyvalue_list:
9+
k,v = keyval.split(':') # (k,1) , (k1,2) , (k2,3) ,(k3,4)
10+
d2[k] = v
11+
12+
13+
print(d2) # {'k': '1 ', 'k1': '2', 'k2': '3', 'k3': '4'}

0 commit comments

Comments
 (0)