如何在列表中查找项目数组并在python中查找下一项

时间:2017-02-14 16:31:37

标签: python

我有一个列表,例如["scissors", "rock", "rock", "paper", "rock" ... (more than 100,000 items)] 并希望在列表中找到一个项目数组,例如["rock", rock", "paper"],找到所有相同的模式,并在所有情况下识别模式后面的下一个项目。

例如,

original list = ["scissors", "rock", "rock", "paper", "rock", "scissors", "rock", "paper", "scissors"] 

我想识别的模式= ["rock", "paper"](上面的列表中有2个)

我正在寻找的模式的最后一个项目=" rock"和"剪刀"。

我怎么能编码?

6 个答案:

答案 0 :(得分:0)

这将收集模式第一部分出现的索引:

original = ["scissors", "rock", "rock", "paper",
            "rock", "scissors", "rock", "paper", "scissors"]

pattern = ["rock", "paper"]

indices = []
for index, item in enumerate(original):
    if item == pattern[0] and index + 1 < len(original):
        if original[index + 1] == pattern[1]:
            indices.append(index)

# where in the original is the pattern found? starting at index...
print(indices)

# how many times was the pattern found?
print(len(indices))

# Result:    
# [[2, 6]
# 2

如果您想查找多个模式并确定每个模式在原始列表中的位置以及频率:

original = ["scissors", "rock", "rock", "paper",
            "rock", "scissors", "rock", "paper", "scissors"]

patterns = [["rock", "paper"], ["scissors", "rock"]]


def look_for_patterns(ori, pat):
    indices = []
    length = len(ori)
    for p in pat:
        sublst = []
        for index, item in enumerate(ori):
            if item == p[0] and index + 1 < length:
                if ori[index + 1] == p[1]:
                    sublst.append(index)
        indices.append(sublst)
    return indices, [len(i) for i in indices]

# where in the original is the pattern found? starting at index...
print(look_for_patterns(original, patterns)[0])

# how many times was the pattern found?
print(look_for_patterns(original, patterns)[1])

# Result:    
# [[2, 6], [0, 5]]
# [2, 2]

答案 1 :(得分:0)

这个怎么样,

def indexofsublist(l, sublist):
    l1, l2 = len(l), len(sublist)
    for idx in range(l1):
        if l[idx:idx+l2] == sublist:
            return idx

original_list = ["scissors", "rock", "rock", "paper", "rock", "scissors", "rock", "paper", "scissors"] 
identify = ["rock", "paper"]

idx = indexofsublist(original_list, identify)   # 2
next_idx = idx + len(identify)                  # 4

答案 2 :(得分:0)

target = ["rock", "paper"]
original = ["scissors", "rock", "rock", "paper", "rock", "scissors", "rock", "paper", "scissors"]

def combinations(iterable, length):
    return [iterable[i: i + length] for i in range(len(iterable) - length + 1)]

combinations = combinations(original, 2)
indices = [i for i, x in enumerate(combinations) if x == target]

for index in indices:
    print(combinations[index+1][-1])

输出:

rock
scissors

代码的作用:

  1. 使用方法combinations打印所有2个连续元素组合
  2. 查找所有出现的索引。
  3. 打印combinations[index+1]的最后一个元素,其中包含您要查找的内容。

答案 3 :(得分:0)

或者这个:

pattern = ["rock", "paper"]
lenpat=len(pattern)
original = ["scissors", "rock", "rock", "paper","rock", "scissors", "rock", "paper", "scissors"]
index=[]
for i in range(len(original)-lenpat):
    if original[i:i+lenpat]==pattern:
        index.append(i)

print original
print pattern
print index

答案 4 :(得分:0)

到目前为止,答案的问题在于它们很棒,但不幸的是每个查询的O(n)。你需要一个后缀trie来回答O(k)中的查询,其中k是模式长度。

这是一个很好的lib,用于相同的https://github.com/ptrus/suffix-trees pip install suffix-trees

然而在你的情况下还需要做更多的处理。因为选择只会来自'摇滚','论文'和'剪刀'(假设蜥蜴和spock以后不加入:-P)规范化并用'r','p'和's'

替换它们

使用“”.join(new_arr)并准备使用上面的github链接。如果您有疑问或想要更多解释,请发表评论。

答案 5 :(得分:0)

如果你有一个很长的列表可供浏览,那么递归可能会被调整(并且更适合它的价值):

cell2mat(out)

返回:

def find_pattern_and_followings(pattern, rest, followings=None,
                                pattern_indexes=None, curr_index=0):
    if len(rest) < len(target):
        return followings, pattern_indexes
    followings = [] if followings is None else followings
    pattern_indexes = [] if pattern_indexes is None else pattern_indexes
    # Check if the first elements match the pattern
    # and move to next elements
    if len(rest) >= len(target):
        if rest[0:len(target)] == target:
            pattern_indexes.append(curr_index)
            if len(rest) > len(target):
                followings.append(rest[len(target)])
            rest = rest[len(target):]
            curr_index += len(target)
        else:
            rest = rest[1:]
            curr_index += 1   
        return(find_pattern_and_followings(pattern, rest, 
                                           followings=followings, 
                                           pattern_indexes=pattern_indexes,
                                           curr_index=curr_index))

该功能的作用是逐项浏览列表,如果列表的第一项与存储有趣信息的模式匹配。然后它会删除已扫描的元素并再次启动,直到列表太短而无法包含模式。