LoginSignup
1
0

More than 5 years have passed since last update.

Pythonの内包記法の練習

Posted at

プログラムチェックで書いた内容の覚書

Before

n=int(input())
rec = {}
for i in range(n):
    r=input().split()
    rec[r[0]]=int(r[1])

After

n = int(input())
rec = dict([[j for j in input().split()] for i in range(n)])

Input

3
foo 1
bar 4
baz 6

メモ

  • 何行にもなる記述を1行に書けるのはコンパクトでPythonステキ
  • しかし記法を覚えないとまるで読めないので、要練習
  • 本当はdict()を使わずに内包記法だけでdictにしたかったが、書き方不明
  • また、keyは文字列でいいけどもvalueは数字なので、intに変換しておきたい
1
0
1

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
1
0