Hi, am having trouble on how to code [False, False, True, True, True, False, True, True, False] into [[2,3],[6,2]] in python

huangapple go评论99阅读模式
英文:

Hi, am having trouble on how to code [False, False, True, True, True, False, True, True, False] into [[2,3],[6,2]] in python

问题

[False, False, True, True, True, False, True, True, False] 编码成 [[2, 3], [6, 2]] 的Python代码应该如何写,其中第一个子列表中的2是True的索引起始位置,3是从其索引开始的True元素的总和。被卡在这个问题上。

英文:

How should I code [False, False, True, True, True, False, True, True, False] into [[2, 3], [6, 2]] in python, where for the first sublist, 2 is where the True index starts and 3 is the sum of True elements from where its index starts.

Stuck with this question

答案1

得分: 1

你可以使用 itertools.groupbyenumerate

  1. from itertools import groupby
  2. [(next(g)[0], 1+len(list(g))) for k, g in groupby(enumerate(lst), key=lambda x: x[1]) if k]
  3. # [(2, 3), (6, 2)]
英文:

You can use itertools.groupbyand enumerate:

  1. >>> from itertools import groupby
  2. [(next(g)[0], 1+len(list(g))) for k, g in groupby(enumerate(lst), key=lambda x: x[1]) if k]
  3. # [(2, 3), (6, 2)]

答案2

得分: 0

使用简单的循环和标志来跟踪上一个状态:

  1. l = [False, False, True, True, True, False, True, True, False]
  2. out = []
  3. previous = False
  4. for i, x in enumerate(l): # 遍历值和位置
  5. if x: # 如果为True
  6. if not previous: # 并且之前为False,添加新的列表
  7. out.append([i, 1])
  8. else: # 否则增加计数器
  9. out[-1][1] += 1
  10. previous = x # 更新下一次迭代的标志

输出:[[2, 3], [6, 2]]

为了好玩,可以使用 itertools.groupby 来实现一行的替代方法:

  1. from itertools import groupby
  2. out = [[(l:=list(g))[0][0], len(l)]
  3. for k, g in groupby(enumerate(l), key=lambda x: x[1]) if k]
英文:

Use a simple loop and a flag to keep track of the last state:

  1. l = [False, False, True, True, True, False, True, True, False]
  2. out = []
  3. previous = False
  4. for i, x in enumerate(l): # iterate over values and positions
  5. if x: # if True
  6. if not previous: # and we had a False before, add new list
  7. out.append([i, 1])
  8. else: # else increment the counter
  9. out[-1][1] += 1
  10. previous = x # update flag for next iteration

Output: [[2, 3], [6, 2]]

For fun, a one-liner alternative using itertools.groupby:

  1. from itertools import groupby
  2. out = [[(l:=list(g))[0][0], len(l)]
  3. for k, g in groupby(enumerate(l), key=lambda x: x[1]) if k]

答案3

得分: -1

检查这段代码

  1. lst = [False, False, True, True, True, False, True, True, False]
  2. result = []
  3. start_index = None
  4. true_count = 0
  5. for i, value in enumerate(lst):
  6. if value:
  7. if start_index is None:
  8. start_index = i
  9. true_count += 1
  10. elif start_index is not None:
  11. result.append([start_index, true_count])
  12. start_index = None
  13. true_count = 0
  14. if start_index is not None:
  15. result.append([start_index, true_count])
  16. print(result)
英文:

check this code

  1. lst = [False, False, True, True, True, False, True, True, False]
  2. result = []
  3. start_index = None
  4. true_count = 0
  5. for i, value in enumerate(lst):
  6. if value:
  7. if start_index is None:
  8. start_index = i
  9. true_count += 1
  10. elif start_index is not None:
  11. result.append([start_index, true_count])
  12. start_index = None
  13. true_count = 0
  14. if start_index is not None:
  15. result.append([start_index, true_count])
  16. print(result)

huangapple
  • 本文由 发表于 2023年7月13日 17:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定