我有一个从另一个列表中的一些项目生成的元组列表。如果第二个元组项是连续的,我希望在列表中获得索引(元组中的第一个项)。
例如,称为“处理”的列表
[(1, 'Chip 1'),
(1, 'Chip 2'),
(1, 'Chip 3'),
(2, 'Chip 4'),
(4, 'Chip 1'),
(4, 'Chip 2'),
(4, 'Chip 3'),
(4, 'Chip 4'),
(5, 'Chip 5'),
(7, 'Chip 1'),
(7, 'Chip 2'),
(7, 'Chip 3'),
(7, 'Chip 4'),
(8, 'Chip 5'),
(10, 'Chip 1'),
(10, 'Chip 2'),
(10, 'Chip 3'),
(12, 'Chip 1'),
(12, 'Chip 2'),
(14, 'Chip 1'),
(16, 'Chip 1'),
(16, 'Chip 2'),
(18, 'Chip 1'),
(18, 'Chip 2'),
(20, 'Chip 1'),
(20, 'Chip 2'),
(20, 'Chip 3'),
(20, 'Chip 4'),
(23, 'Chip 1'),
(25, 'Chip 1'),
(27, 'Chip 1'),
(27, 'Chip 2')]我可以通过使用more_itertools获得顺序列表中的芯片,但我不知道如何进一步处理。有人能帮我弄清楚吗?
import more_itertools as mit
chip_nums = [int(p[1][-1]) for p in processed]
for group in mit.consecutive_groups(chip_nums):
print(list(group))
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3]
[1, 2]
[1]
[1, 2]
[1, 2]
[1, 2, 3, 4]
[1]
[1]
[1, 2]发布于 2021-03-04 12:20:34
groups包含processed列表中的元素索引(起始和结束包含),这些索引是连续的。
注释:某些元组包含相同/相等的开始值和结束值,这意味着没有连续性。
groups = []
start = 0
for i in range(len(processed) - 1):
if int(processed[i][1].split()[-1]) + 1 == int(
processed[i + 1][1].split()[-1]
): # checks if the next element is 1 greater than previous element
pass
else:
groups.append((start, i)) # stores the start and ending index of a continuous group
start = i + 1
groups.append((start, i + 1)) # this handles the last remaining element
index_list = [[item[0] for item in processed[start : end + 1]] for start, end in groups]输出:
[[1, 1, 1, 2], [4, 4, 4, 4, 5], [7, 7, 7, 7, 8], [10, 10, 10], [12, 12], [14], [16, 16], [18, 18], [20, 20, 20, 20], [23], [25], [27, 27]]发布于 2021-03-04 12:12:40
你可以这样做:
(在这段代码之后你会找到一个解释)
from itertools import groupby
from operator import itemgetter
# PART 1
# List that will contain lists of indexes of "chip_nums".
# Each list of indexes will correspond to consecutive chips values
list_of_indexes =list()
for k,g in groupby(enumerate(chip_nums),lambda x:x[0]-x[1]):
group = (map(itemgetter(0),g))
group = list(map(int,group))
list_of_indexes.append(group)
# PART 2
# From the retrieved indexes, get fist value in tuple ("index in tuple")
list_of_indexes_contained_in_tuples = list()
for indexes in list_of_indexes:
consecutive_indexes = list()
for index in indexes:
tuple_in_processed = processed[index]
index_in_tuple = tuple_in_processed[0]
consecutive_indexes.append(index_in_tuple)
list_of_indexes_contained_in_tuples.append(consecutive_indexes)
print(list_of_indexes_contained_in_tuples)输出:
[[1, 1, 1, 2], [4, 4, 4, 4, 5], [7, 7, 7, 7, 8], [10, 10, 10], [12, 12], [14], [16, 16], [18, 18], [20, 20, 20, 20], [23], [25], [27, 27]]解释:
“第1部分”是来自more_itertools的函数more_itertools的修改版本。我不知道您是否知道,consecutive_groups (https://github.com/more-itertools/more-itertools/blob/master/more_itertools/more.py)的源代码是在Python.org上可以找到的一个实现:https://docs.python.org/2.6/library/itertools.html#examples
这个食谱最初是用Python2.6编写的:
from operator import itemgetter
from itertools import groupby
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda (i,x):i-x):
print map(itemgetter(1), g)在Python 3中进行转置,您将得到以下内容:
from operator import itemgetter
from itertools import groupby
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda x:x[0]-x[1]):
print(list(map(itemgetter(1), g)))修改包括将lambda函数改编为Python3。注意,为了获得映射对象的列表对象insead,我们还用list()包装了list()函数。
现在,通过这个函数,我们可以得到您所提供的代码。
输出:
[2, 3, 4, 5]
[12, 13, 14, 15, 16, 17]现在,我们将修改这个函数,以获取元素的索引,而不是列表中的值(此处命名为"data")。为此,我们只需将0作为参数传递给itemgetter而不是1。
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda x:x[0]-x[1]):
print(list(map(itemgetter(0), g)))不,我们有索引。
输出:
[0, 1, 2, 3]
[4, 5, 6, 7, 8, 9]第二部分只使用索引来获取元组中的第一项(所以是您想要的索引)。
因此,简单地说,我调整了来自more_itertools的原始函数more_itertools,以返回项的索引而不是它们的值,并使用这些索引检索每个元组中的第一个值。
发布于 2021-03-04 12:38:59
使用itertools和operator模块的游行:
from itertools import islice
from operator import itemgetter
import more_itertools as mit
it = iter(processed)
getter = itemgetter(0)
chip_nums = map(lambda x: int(x[1][-1]), processed)
for group in mit.consecutive_groups(chip_nums):
print([*islice(map(getter, it), len([*group]))])输出:
[1, 1, 1, 2]
[4, 4, 4, 4, 5]
[7, 7, 7, 7, 8]
[10, 10, 10]
[12, 12]
[14]
[16, 16]
[18, 18]
[20, 20, 20, 20]
[23]
[25]
[27, 27]https://stackoverflow.com/questions/66474072
复制相似问题