0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

三項演算子、lambda、リスト内表記、辞書のget

0
Posted at

よく使うけど難しい文法

三項演算子

player = obj.get("player",0) if isinstance(obj, dict) else obj.player

普通のifで記述すると

if isinstance(obj, dict):
  player = obj.get("player",0)
else:
  player = obj.player

lambda

double = lambda x: x * 2

value = double(3)
def double(x):
  return x * 2

value = double(3)

リスト内表記

new_list = [p for p in list if p.owner == player]

分解すると

new_list = []

for p in list:
  if p.owner == player:
    new_list.append(p)

辞書のget

player = obj.get("player", 0)

キーが存在しない時0を返す

if "player" in obj:
    player = obj["player"]
else:
    player = 0
0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?