1
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 3 years have passed since last update.

Perlにおけるhash(ハッシュ)は、Pythonにおける辞書(ディクショナリ)

Last updated at Posted at 2020-04-11

Perlにおけるhashは、Pythonにおける辞書

・Key(キー)とvalue(値)をセットで格納する。

正直、私はPerlのhashの機能はよく知らない。

Pythonで辞書は
x = {"book":"Yomitai", "hon":3}
のように、key:valueを{}の中で定義する。

kiri.py
kiri = {"price":20, "number":12}
for key in kiri:
    print(key+":"+str(kiri[key]))

for key, value in kiri.items():
のように、itemsメソッドを使うと、辞書のキーと値をセットで取得できる。
その時は、str(kiri[key])では無くて、str(value)で良い。

更に、formatを使うと簡潔にできる。

formatkiri.py
kiri = {"price":20, "number":12}
for key, value in kiri.items():
    print("{0}:{1}".format(key, value))

(書き足すかも)

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