Written by elevenlab employee

[Python] 内包表記集

一次配列 + 内包表記

# 通常
result = []
for x in range(10):
    result.append(x)
print(result)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



# 内包表記
result = [x for x in range(10)]
print(result)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

if + 内包表記

# 通常
result = []
for x in range(10):
    if x % 2 == 0:
        result.append(x)
print(result)
# [0, 2, 4, 6, 8]



# 内包表記
result = [x for x in range(10) if x % 2 == 0]
print(result)
# [0, 2, 4, 6, 8]

if + else + 内包表記

# 通常
result = []
for x in range(10):
    if x % 2 == 0:
        result.append(x)
    else:
        result.append(x + 100)
print(result)
# [0, 101, 2, 103, 4, 105, 6, 107, 8, 109]



# 内包表記
result = [x if x % 2 == 0 else x + 100 for x in range(10) ]
print(result)
# [0, 101, 2, 103, 4, 105, 6, 107, 8, 109]

二次配列 + 内包表記

# 通常
result = []
for a in range(10):
    tmp = []
    for _ in range(16):
        tmp.append(a)
    result.append(tmp)
print(result)



# 内包表記
result = [[a for _ in range(16)] for a in range(10)]
print(result)
# [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
#  [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
#  [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
#  [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
#  [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
#  [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
#  [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
#  [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
#  [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9]]

二次配列 + 変形 + 内包表記

# 通常
width_array = []
for a in range(6):
    tmp = []
    for _ in range(16):
        tmp.append(a)
    width_array.append(tmp)
    
print(width_array)


# 変形
height_array = []
for i in range(len(width_array[0])):
    tmp = []
    for w in width_array:
        tmp.append(w[i])
    height_array.append(tmp)
    
print(height_array)



# 内包表記
width_array = [[a for _ in range(16)] for a in range(6)]
print(width_array)
# [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
#  [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
#  [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
#  [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
#  [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]]


# 変形
height_array = [[w[i] for w in width_array] for i in range(len(width_array[0]))]
print(height_array)
# [[0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5],
#  [0, 1, 2, 3, 4, 5]]

class + 内包表記

import random

# 購入した商品の情報を保持するクラス
class Customer:
    purchases = None
    
    def __init__(self, purchases):
        self.purchases = purchases
        
    def get_purchases(self):
        return self.purchases

    
# {商品名 : 値段}
goods_dict = {'fruit': 200, 'drink': 120, 'vegetable': 70, 'snack': 100}

# [商品名]
goods_name = list(goods_dict.keys())

# ランダムに5つの商品を購入したお客様を20名作成
customers = [Customer(random.choices(goods_name, k=5)) for _ in range(20)]

# 一人目のお客様の購入した商品名から値段を取得
price_list = [goods_dict[name] for name in customers[0].get_purchases()]

print(customers[0].get_purchases()) 
# ['drink', 'snack', 'fruit', 'fruit', 'drink']

print(price_list) 
# [120, 100, 200, 200, 120]

print("総額: ", sum(price_list))
# 総額: 740

#  全てのお客様の購入した商品名から値段を取得
price_list = [[goods_dict[name] for name in custmer.get_purchases()] for custmer in customers]
_ = [print(f'{i+1}人目: ', sum(price)) for i, price in enumerate(price_list)]
# 1人目:  740
# 2人目:  710
# 3人目:  690
# ...
# 18人目:  610
# 19人目:  630
# 20人目:  560
この記事を書いた人

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です