繁体   English   中英

如何在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是否出现在Data中。 我真正需要的是Data中每次出现后的值。

是否有人对如何解决这个问题有任何想法? 谢谢!

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

您返回的生成器生成以下列表的结果:

[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匹配。 仅当列表中的所有元素均为Trueall返回True ,因此,仅当双胞胎数字匹配CurrentTuple[all(x) for x in ...]生成的列表才会包含True 当使用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任何替代方法都快得多(请参阅[ 比较列表推导和显式循环(了解3个数组生成器,比1个循环快)以了解原因)。

您可以使用字典将数据分组,以查看原始列表中的梳子在哪里/是否在拉链对中到达:

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()))

您可以将其应用于您喜欢的任何实现,只要生成元组,逻辑就相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM