如何使用 itertools groupby 进行打印?
How to print with itertools groupby?
我有这个代码:
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
group = groupby(text)
for k, g in group:
print(k, end= " ")
print(sum(1 for _ in g), end=" ")
我需要的示例:
A B C
2 2 1
我的itertools
只显示这样:
A 2 B 2 C 1
中使用多个打印语句
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
group = groupby(text)
# Transpose so we have row 0 with group names, and row 1 with grouping items
transposed = [(k, list(g)) for k, g in group]
for k in transposed:
print(k[0], end = " ")
print()
for k in transposed:
print(sum(1 for _ in k[1]), end=" ")
您可以这样做,post-处理结果 groupby
returns 以便轻松获取输出的每一行所需的值:
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
groups = [(k, str(sum(1 for _ in g))) for k, g in groupby(text)]
a, b = zip(*groups)
print(' '.join(a)) # -> a b c
print(' '.join(b)) # -> 2 2 1
这是另一种选择:
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
group = groupby(text)
dictionary = {}
for k, g in group:
dictionary[k] = sum(1 for _ in g)
keys = " ".join(list(dictionary.keys()))
values = " ".join(str(v) for v in list(dictionary.values()))
print(keys)
print(values)
我知道你问过 itertools.groupby,但我会使用计数器来完成这样的任务:
>>> text = ["a", "a", "b", "b", "c"]
>>> from collections import Counter
>>> c = Counter(text)
>>> print(c)
Counter({'a': 2, 'b': 2, 'c': 1})
>>> headers = c.keys()
>>> values = [str(val) for val in c.values()]
>>> print(' '.join(headers))
a b c
>>> print(' '.join(values))
2 2 1
由于 groupby()
不适合部署 tee()
来制作副本,
最好的解决方案似乎是创建一个元组理解。注意
我们对每个组 g 中包含的值不感兴趣,所以我们只
存储从组中动态构建的元组的长度。
import itertools
text = ["a", "a", "b", "b", "c"]
group = tuple((k,len(tuple(v))) for k,v in itertools.groupby(text))
for t in group:
print(t[0], end= " ")
print()
for t in group:
print(t[1], end=" ")
print()
# a b c
# 2 2 1
我有这个代码:
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
group = groupby(text)
for k, g in group:
print(k, end= " ")
print(sum(1 for _ in g), end=" ")
我需要的示例:
A B C
2 2 1
我的itertools
只显示这样:
A 2 B 2 C 1
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
group = groupby(text)
# Transpose so we have row 0 with group names, and row 1 with grouping items
transposed = [(k, list(g)) for k, g in group]
for k in transposed:
print(k[0], end = " ")
print()
for k in transposed:
print(sum(1 for _ in k[1]), end=" ")
您可以这样做,post-处理结果 groupby
returns 以便轻松获取输出的每一行所需的值:
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
groups = [(k, str(sum(1 for _ in g))) for k, g in groupby(text)]
a, b = zip(*groups)
print(' '.join(a)) # -> a b c
print(' '.join(b)) # -> 2 2 1
这是另一种选择:
from itertools import groupby
text = ["a", "a", "b", "b", "c"]
group = groupby(text)
dictionary = {}
for k, g in group:
dictionary[k] = sum(1 for _ in g)
keys = " ".join(list(dictionary.keys()))
values = " ".join(str(v) for v in list(dictionary.values()))
print(keys)
print(values)
我知道你问过 itertools.groupby,但我会使用计数器来完成这样的任务:
>>> text = ["a", "a", "b", "b", "c"]
>>> from collections import Counter
>>> c = Counter(text)
>>> print(c)
Counter({'a': 2, 'b': 2, 'c': 1})
>>> headers = c.keys()
>>> values = [str(val) for val in c.values()]
>>> print(' '.join(headers))
a b c
>>> print(' '.join(values))
2 2 1
由于 groupby()
不适合部署 tee()
来制作副本,
最好的解决方案似乎是创建一个元组理解。注意
我们对每个组 g 中包含的值不感兴趣,所以我们只
存储从组中动态构建的元组的长度。
import itertools
text = ["a", "a", "b", "b", "c"]
group = tuple((k,len(tuple(v))) for k,v in itertools.groupby(text))
for t in group:
print(t[0], end= " ")
print()
for t in group:
print(t[1], end=" ")
print()
# a b c
# 2 2 1