2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで辞書を作成する

Last updated at Posted at 2019-12-12

はじめに

 Pythonの勉強メモです。今回は辞書と内包記号を学びました。

コード

 特定の記号で文字列を区切って辞書にします。

dict.py
import re

strings = "1:a 3:c 4:d"            # 辞書にしたい文字列
splited = re.split(" +", strings)  # スペースで分割
lists = []                         #キーと値を格納

for char in splited:    # コロンで分割後リストに追加
    tmp = char.split(":")
    lists.append(tmp)

dict = {k: v for (k, v) in lists}  # 辞書作成
print(dict)

学び

 文字列を特定のパターンで分離する方法を学びました。

おわりに

 もっとスッキリと書けるようになりたいです...
 ご覧いただけありがとうございました。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?