質問するログイン新規登録

回答編集履歴

3

リストをタプルに変更

2022/03/04 16:21

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -19,10 +19,10 @@
19
19
  names.append(name)
20
20
  counts += count
21
21
  if counts >= limit:
22
- yield [store, " ".join(names)]
22
+ yield store, " ".join(names)
23
23
  names, counts = [], 0
24
24
  if names:
25
- yield [store, " ".join(names)]
25
+ yield store, " ".join(names)
26
26
 
27
27
  for item in convert():
28
28
  print(item)

2

処理簡略化

2022/03/04 16:08

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -14,15 +14,13 @@
14
14
 
15
15
  def convert(limit=5):
16
16
  for store, products in stores.items():
17
- names = []
17
+ names, counts = [], 0
18
- counts = 0
19
18
  for name, count in products.items():
20
19
  names.append(name)
21
20
  counts += count
22
21
  if counts >= limit:
23
22
  yield [store, " ".join(names)]
24
- counts = 0
23
+ names, counts = [], 0
25
- names = []
26
24
  if names:
27
25
  yield [store, " ".join(names)]
28
26
 

1

処理簡略化

2022/03/04 16:06

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -14,22 +14,17 @@
14
14
 
15
15
  def convert(limit=5):
16
16
  for store, products in stores.items():
17
- items = iter(products.items())
17
+ names = []
18
+ counts = 0
18
- for name, counts in items:
19
+ for name, count in products.items():
20
+ names.append(name)
21
+ counts += count
19
22
  if counts >= limit:
20
- yield [store, name]
21
- continue
22
- names = [name]
23
- for name, count in items:
24
- names.append(name)
25
- counts += count
26
- if counts >= limit:
27
- yield [store, " ".join(names)]
28
- counts = 0
29
- names = []
30
- if names:
31
23
  yield [store, " ".join(names)]
24
+ counts = 0
25
+ names = []
32
- break
26
+ if names:
27
+ yield [store, " ".join(names)]
33
28
 
34
29
  for item in convert():
35
30
  print(item)