回答編集履歴

1

2024/07/10 13:06

投稿

melian
melian

スコア20283

test CHANGED
@@ -1,15 +1,19 @@
1
- `collections.ChainMap()` を利用する場合です。
1
+ > #追加3
2
+
2
3
  ```python
3
- from collections import ChainMap
4
+ fruit_dict = [
4
- name, price, production_area = map(lambda x: dict(ChainMap(*x[::-1])),
5
+ {"商品名1": ['みかん', '100円', '沖縄']},
5
- zip(*[[{k: v} for k, v in d.items()] for d in fruit_dict]))
6
+ {"商品名2": ['りんご', '200円', '沖縄']},
7
+ ]
6
8
 
7
- print(name)
8
- print(price)
9
- print(production_area)
9
+ name, price, production_area = map(dict, zip(*[[(k, i) for k, v in d.items() for i in v] for d in fruit_dict]))
10
10
 
11
+ print(f'{name = }')
12
+ print(f'{price = }')
13
+ print(f'{production_area = }')
14
+
11
- # {'商品名1': 'みかん', '商品名2': 'りんご'}
15
+ # name = {'商品名1': 'みかん', '商品名2': 'りんご'}
12
- # {'価格1': '100円', '価格2': '200円'}
16
+ # price = {'商品名1': '100円', '商品名2': '200円'}
13
- # {'産地1': '北海道', '産地2': '沖縄'}
17
+ # production_area = {'商品名1': '沖縄', '商品名2': '沖縄'}
14
18
  ```
15
19