gpt4 book ai didi

python - 在嵌套的 For 循环中查找元素

转载 作者:太空狗 更新时间:2023-10-30 02:07:22 24 4
gpt4 key购买 nike

我的代码中有两个名为 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

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

最佳答案

你也可以这样做

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

关于python - 在嵌套的 For 循环中查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51617624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com