Create a dictionary in Python

Introduction

This is a Python study memo. This time I learned dictionaries and comprehension symbols.

code

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)

Learn

I learned how to separate strings in a specific pattern.

in conclusion

I want to be able to write more clearly ... Thank you for visiting.

Recommended Posts

Create a dictionary in Python
Create a function in Python
Create a DI Container in Python
Create a binary file in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a Python module
Create SpatiaLite in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
Hash in Perl is a dictionary in Python
[GPS] Create a kml file in Python
Create a Python environment
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Create a package containing global commands in Python
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Take a screenshot in Python
Create a Wox plugin (Python)
Create gif video in Python
Make a bookmarklet in Python
Create a python numpy array
Avoid KeyError in python dictionary
Draw a heart in Python
Create a directory with python
What is the fastest way to create a reverse dictionary in python?
Create a data collection bot in Python using Selenium
Receive dictionary data from a Python program in AppleScript
[LINE Messaging API] Create a rich menu in Python
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Python dictionary
Create a fake Minecraft server in Python with Quarry
[Python] dictionary
Python dictionary
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a CSV reader in Flask
Create a python GUI using tkinter
Create a Python environment on Mac (2017/4)
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Create a nested dictionary using defaultdict
Create a virtual environment with Python!
Write A * (A-star) algorithm in Python
Create Gmail in Python without API
Solve ABC036 A ~ C in Python
Create a python environment on centos
Write a pie chart in Python