在嵌套循环中查找元素

时间:2018-07-31 16:01:56

标签: python for-loop

我的代码中有两个名为extrabe的列表。

extra的输出是[7,27]

be的输出是

'Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']

我需要找到extra的第7和27个单词元素(即,如果元素为空字符串,则不计算在内)。应该是Nonsocial PlayGroom,但是for循环中我只打印Nonsocial Play

这些是我正在使用的for循环:

for x in extra:
count = 0
for y in be:
    if y != '':
        if x == count:
            print(be[x])
            count += 1
        elif x != count:
            count += 1

如果您知道为什么它不起作用,请告诉我! 编辑:我想打印这些语句,但我也需要删除它们

8 个答案:

答案 0 :(得分:2)

我创建了简单的函数filter_iter(itr),该函数从所有空值中过滤了参数itr中的可迭代项。然后,您可以使用列表extra中的值访问生成的过滤列表:

extra = [7,27]
be = ['Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']

def filter_iter(itr):
    return [i for i in itr if i]

be = filter_iter(be)
print([be[e] for e in extra])

打印:

['Nonsocial Play', 'Groom']

答案 1 :(得分:2)

您也可以

extra = [7,27]
be = ['Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']

>>> [x for i, x in enumerate(s for s in be if s) if i in extra]
['Nonsocial Play', 'Groom']

答案 2 :(得分:1)

Python 2

new_be=filter(lambda a: a != '', be) #we delete from the list all the '' and we save the new list in new_be
world=[]
for i in extra:
    world.append(new_be[int(i)])

print (world)

Python 3

    new_be=list(filter(("").__ne__, be)) #we delete from the list all the '' and we save the new list in new_be
    world=[]
    for i in extra:
        world.append(new_be[int(i)])

    print (world)
  

['非社交游戏','新郎']

答案 3 :(得分:1)

使用list comprehensionenumeratefilter的单行代码

[j for i, j in enumerate(list(filter(lambda x: x != '', be))) if i in extra]
['Nonsocial Play', 'Groom']

测试速度:

%timeit [j for i, j in enumerate(list(filter(lambda x: x != '', be))) if i in extra]
45.2 µs ± 5.48 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

答案 4 :(得分:1)

count到达x时,打印be中的当前项目

for x in extra:
    count = 0
    for y in be:
        if y != '':
            if x == count:
                print(y)
                #print(be[x])
                count += 1
            elif x != count:
                count += 1

在序列上进行迭代时删除序列中的项目无效。
制作extrabe中的deques,然后使用Rotate方法访问 be中的项目。设计逻辑,以便在满足条件时弹出be个项目,并在找到extra个项目时弹出be个项目。跟踪旋转,以便可以重建顺序。

extra.sort()
extraa = collections.deque(extra)
bee = collections.deque(be)
#print(len(bee))
rotation_count = 0
word_count = 0
while extraa:
    while bee[0] == '':
        bee.rotate(-1)
        rotation_count += 1
    if word_count == extraa[0]:
        print(bee.popleft())
        extraa.popleft()
    else:
        bee.rotate(-1)
        rotation_count += 1
    word_count += 1
#print(len(bee))

bee.rotate(rotation_count)
be = list(bee)

答案 5 :(得分:1)

这是一种解决方案,其中使用filter元素,enumerate对结果迭代器及其索引进行迭代,并itertools.islice仅从其中获取一定范围的索引

>>> from itertools import islice
>>> extra = [7,27]
>>> be = ['Feed Forage', '', '', '', 'Social Play', '', 'Feed Forage', 'Nonsocial Play', '', 'Nonsocial Play', '', '', '', '', 'Nonsocial Play', '', '', '', '', '', '', '', '', 'Social Play', '', '', '', '', '', '', '', '', 'Nonsocial Play', 'Groom', 'Feed Forage', '', '', 'Nonsocial Play', '', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', 'Social Play', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', '', '', 'Feed Forage', '', '', '', '', '', 'Feed Forage', '', '', '', 'Groom', 'Groom', '', '', '', '', 'Groom', 'Groom', 'Groom', '', 'Groom', '', '', '', 'Feed Forage', '', '', 'Feed Forage', 'Nonsocial Play', '', '', '', 'Pace', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', 'Feed Forage', '', 'Social Play', '', 'Feed Forage', '', 'Social Play', '', 'Social Play', '', '', '', '', '', 'Nonsocial Play', '', 'Social Play', '', '', '', '', 'Feed Forage', '', '', '', '', '', '', '', 'Nonsocial Play', '', 'Nonsocial Play', '', 'Feed Forage', 'Social Play', '', '', '', 'Feed Forage', '', 'Nonsocial Play', '', '', 'Feed Forage', '', '', '', '', 'Self Groom', '', '', 'Groom', '', '', 'Self Groom', '', '', '', '', '', 'Groom', '', '', 'Self Groom', '', '', 'Feed Forage', '', '', '', '', '', 'Pace', '', 'Self Groom', '', '', '', 'Self Groom', '', 'Self Groom', '', 'Social Play', '', 'Social Play', '', 'Self Groom', 'Groom', '', '', 'Groom', '', 'Groom', '', 'Groom', 'Groom', 'Groom', '', '', 'Self Groom', '', 'Groom', '', 'Groom', '', 'Feed Forage', '', 'Feed Forage', '', 'Feed Forage', '', '', '', '', '', '', '', 'Feed Forage', '', '', '', '', 'Feed Forage', 'Nonsocial Play', '', '', 'Self Groom', 'Nonsocial Play', '', '', '', 'Groom', '', '', 'Groom', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', 'Groom', '', '', '', '', '', '', 'Groom', 'Groom', '', 'Groom', '', '', 'Groom', '', '', '', 'Self Groom', '', '', '', '', '', '', 'Groom', '', 'Groom', '', 'Feed Forage', '']
>>> [x for i,x in enumerate(islice(filter(bool, be), min(extra), max(extra)+1), min(extra)) if i in extra]
['Nonsocial Play', 'Groom']

答案 6 :(得分:0)

由于缩进不正确而无法正常工作。一旦找到该元素,最好包含一个break,因为在找到第一个元素之后要开始新的计数。您还在打印be [x]哪个错误。应该是y,因为y是要打印的字符串。

for x in extra:
    count = 0
    for y in be:
        if y != '':
            if x == count:
                print(y)
                count += 1
                break
            elif x != count:
                count += 1

答案 7 :(得分:0)

此:

[[w for w in be if w != ''][k] for k in extra]