在嵌套的ForLoop中查找元素

2024-05-18 07:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中有两个名为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循环只有prints 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

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


Tags: inself元素forplayiffeedcount
3条回答

使用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)

你也可以这样做

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']

我创建了一个简单的函数filter_iter(itr),它从任何空值中过滤参数itr中的iterable。然后可以使用listextra中的值访问得到的筛选列表:

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']

相关问题 更多 >