This is a Python study memo. This time I learned dictionaries and comprehension symbols.
Make a dictionary by separating character strings with specific symbols.
dict.py
import re
strings = "1:a 3:c 4:d" #The character string you want to make into a dictionary
splited = re.split(" +", strings) #Split by space
lists = [] #Store key and value
for char in splited: #Add to list after split with colon
tmp = char.split(":")
lists.append(tmp)
dict = {k: v for (k, v) in lists} #Dictionary creation
print(dict)
I learned how to separate strings in a specific pattern.
I want to be able to write more clearly ... Thank you for visiting.
Recommended Posts