如何找到相对于列表在 Python 列表中出现位置的值?

How do I find a value relative to where a list occurs within my list in Python?

我有一个号码列表:

Data = [0,2,0,1,2,1,0,2,0,2,0,1,2,0,2,1,1,...]

我有一个二元组列表,它是上面各个数字的所有可能组合:

Combinations = [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)]

我想尝试查找组合中的每个项目出现在数据中的位置,并将每次出现后的值添加到另一个列表。

例如,对于 (0,2),我想创建一个列表 [0,0,0,1],因为这些值是在数据中出现 (0,2) 之后立即下降的值。

到目前为止,我有:

any(Data[i:i+len(CurrentTuple)] == CurrentTuple for i in xrange(len(Data)-len(CurrentTuple)+1))

其中 CurrentTupleCombinations.pop()。 问题是这只给我一个布尔值,判断 CurrentTuple 是否出现在数据中。我真正需要的是数据中每次出现后的值。

有人知道如何解决这个问题吗?谢谢!

sum([all(x) for x in (Data[i:i+len(CurrentTuple)] == CurrentTuple for i in xrange
(len(Data)-len(CurrentTuple)+1))])

你做了什么 return 生成以下列表的生成器:

[array([False,  True], dtype=bool),
 array([ True, False], dtype=bool),
 array([False,  True], dtype=bool),
 ...
 array([False, False], dtype=bool),
 array([False, False], dtype=bool),
 array([False, False], dtype=bool),
 array([False,  True], dtype=bool)]

仅当数组中的 bool 均为 True 时,此列表中的其中一个数组才与 CurrentTuple 匹配。 all return True 仅当列表的所有元素都是 True 因此 [all(x) for x in ...] 生成的列表将包含 True 只有当数字的双胞胎匹配 CurrentTuple。当您使用 sum 时,True 被视为 1。我希望它是清楚的。

如果您只想比较非重叠对:

[2,2,
 0,2,
 ...]

并使算法尽可能通用,您可以使用以下内容:

sum([all(x) for x in (Data[i:i+len(CurrentTuple)] == CurrentTuple for i in xrange
(0,len(Data)-len(CurrentTuple)+1,len(CurrentTuple)))])

尽管更加神秘,但这段代码比使用 append 的任何替代方法都要快得多(查看 [[=​​27=] 以了解原因)。

您可以使用字典对数据进行分组以查看 where/if 原始列表中的任何梳子区域压缩成对:

it1, it2 = iter(Data), iter(Data)
next(it2)

Combinations = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

d = {c: [] for c in Combinations}
ind = 2
for i, j in zip(it1, it2):
    if (i, j) in d and ind < len(Data):
        d[(i, j)].append(Data[ind])
    ind += 1
print(d)

哪个会给你:

{(0, 1): [2, 2], (1, 2): [1, 0], (0, 0): [], (2, 1): [0, 1], (1, 1): [2], (2, 0): [1, 2, 1, 2], (2, 2): [], (1, 0): [2], (0, 2): [0, 0, 0, 1]}

你也可以反过来做:

from collections import defaultdict

it1, it2 = iter(Data), iter(Data)
next(it2)
next_ele_dict = defaultdict(list)
data_iter = iter(Data[2:])
for ind, (i, j) in enumerate(zip(it1, it2)):
    if ind < len(Data) -2:
      next_ele_dict[(i, j)].append(next(data_iter))

def next_ele():
    for comb in set(Combinations):
        if comb in next_ele_dict:
           yield comb, next_ele_dict[comb]

print(list(next_ele()))

哪个会给你:

 [((0, 1), [2, 2]), ((1, 2), [1, 0]), ((2, 1), [0, 1]), ((1, 1), [2]), ((2, 0), [1, 2, 1, 2]), ((1, 0), [2]), ((0, 2), [0, 0, 0, 1])]

任何方法都比为组合中的每个元素传递数据列表更好。

要处理任意长度的元组,我们只需要根据长度创建元组:

from collections import defaultdict

n = 2

next_ele_dict = defaultdict(list)

def chunks(iterable, n):
    for i in range(len(iterable)-n):
        yield tuple(iterable[i:i+n])

data_iter = iter(Data[n:])
for tup in chunks(Data, n):
      next_ele_dict[tup].append(next(data_iter))

def next_ele():
    for comb in set(Combinations):
        if comb in next_ele_dict:
            yield comb, next_ele_dict[comb]


print(list(next_ele()))

您可以将它应用于您喜欢的任何实现,就制作元组而言,逻辑将是相同的。