在列表中查找所有匹配项

时间:2018-09-11 11:51:00

标签: python list

我正在尝试列出列表中某个元素的所有匹配项。但是弄错了。下面的代码有什么问题。

theList = ['a','e','i','o','u','e','o','e']

def matchall1(theList, value, pos=0):
    loc = pos - 1
    try:
        loc = theList.index(value, loc+1)
        yield loc
    except ValueError:
        pass

value = 'e'
for loc in matchall1(theList, value):
    print("match at", loc+1, "position.")

我从上面的代码中得到的结果只是“在2位匹配。”

6 个答案:

答案 0 :(得分:2)

这可能很简单,就像您忘记了循环

def matchall1(theList, value, pos=0):
    loc = pos - 1
    try:
        while True:  # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            loc = theList.index(value, loc+1)
            yield loc
    except ValueError:
        pass

value = 'e'
for loc in matchall1(theList, value):
    print("match at", loc+1, "position.")

输出:

match at 2 position.
match at 6 position.
match at 8 position.

答案 1 :(得分:1)

如果要查找所有匹配项,则需要某种形式的重复(迭代循环或递归)。

根据功能的签名判断,您打算使用递归,该递归看起来像:

pyspark.sql.utils.AnalysisException: "Reference 'id_col' is ambiguous, could be: id_col, id_col.;"

使用循环,您的代码将如下所示:

def matchall_version1(theList, value, i=0):
    try:
        i = theList.index(value, i)
        yield i
        yield from matchall_version1(theList, value, i+1)
    except ValueError:
        pass

但是我想建议这个版本,它比您的恕我直言更具可读性:

def matchall_version2(theList, value):
    i = 0
    try:
        while True:
            i = theList.index(value, i + 1)
            yield i
    except ValueError:
        pass

所有三个版本的结果相同。这个:

def matchall_version3(theList, value):
    for i, x in enumerate(theList):
        if x == value:
            yield i

打印此内容:

theList = ['a','e','i','o','u','e','o','e']
print(list(matchall_version1(theList, 'e')))
print(list(matchall_version2(theList, 'e')))
print(list(matchall_version3(theList, 'e')))

答案 2 :(得分:0)

您可以通过列表理解来实现

[index for index, letter in enumerate(theList) if letter==value]

答案 3 :(得分:0)

算法有点混乱,最好使用生成器表达式:

theList = ['a','e','i','o','u','e','o','e']
def matchAll(element, lst):
    yield from (i for i, e in enumerate(lst) if e == element)

这里有live example

答案 4 :(得分:0)

对于您提出的这类问题,我将不做详细介绍,相反,我会给您一个可以帮助您解决问题的答案。

theList = ['a','e','i','o','u','e','o','e']

def matchall1(theList, value, pos=0):
    loc = pos - 1
    try:
        for items in theList:  #ADD THIS TO YOUR CODE AND YOU'LL BE GOOD
            loc = theList.index(value, loc+1)
            yield loc
    except ValueError:
        pass

value = 'e'
for loc in matchall1(theList, value):
    print("match at", loc+1, "position.")

这将为您提供全面的输出,即一次输出。如果您希望所有位置都在同一输出语句中,那么建议您将其添加到列表中,然后将其传递到稍后实现的for循环中。

编辑1: 我快速添加了可以在同一条语句中输出所有位置的部分。该代码可能看起来很讨厌,并且非专业,但它可以工作。看看吧

theList = ['a','e','i','o','u','e','o','e']

def matchall1(theList, value, pos=0):
    loc_list = []
    loc = pos - 1
    try:
        for items in theList:
            loc = theList.index(value, loc+1)
            loc_list.append(loc)
            yield loc_list
    except ValueError:
        pass

value = 'e'
for loc in matchall1(theList, value):
    continue
new_loc = []
for x in loc:
    x+=1
    new_loc.append(x)

pos = ",".join(str(x) for x in new_loc)

print("Match at", pos, "position.")

输出:

Match at 2,6,8 position.

答案 5 :(得分:-1)

theList = ['a','e','i','o','u','e','o','e']

def matchall1(theList, value, pos=0):
    loc = pos - 1
    loc = [i for i, v in enumerate(theList) if v == value]
    for p in loc:
        yield p

value = 'e'
for loc in matchall1(theList, value):
    print("match at", loc+1, "position.")