Python 如何找到缺失元素的索引?

Python 如何找到缺失元素的索引?,python,list,Python,List,假设您有以下列表: fruit_types = ["apple","pear","strawberry","mango"] fruits = ["apple","strawberry","mango"] 我该如何编写一个循环来标识“pear”元素在 “水果”列表的第二个索引 我知道第一步是从: for element in fruit_types: if element not in fruits: 但是我完全不知道从这里还能往哪里走。您可以使用集合上的差分操作来找到这个 fruit

假设您有以下列表:

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]
我该如何编写一个循环来标识“pear”元素在 “水果”列表的第二个索引

我知道第一步是从:

for element in fruit_types:
    if element not in fruits:

但是我完全不知道从这里还能往哪里走。

您可以使用
集合上的差分操作来找到这个

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]

missings = list(set(fruits_types) - set(fruit))
print missings
如果这很酷,那么为什么要使用循环呢

然后,要获取缺少项的索引,请执行以下操作:

for missing in missings:
    print fruit_types.index(missing)

您可以使用
set
上的差分操作来查找此项

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]

missings = list(set(fruits_types) - set(fruit))
print missings
如果这很酷,那么为什么要使用循环呢

然后,要获取缺少项的索引,请执行以下操作:

for missing in missings:
    print fruit_types.index(missing)

使用集合差异,并查找索引:

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]

missings = set(fruit_types) - set(fruits)
for missing in missings:
    print(fruit_types.index(missing))
输出:
使用集合差异,并查找索引:

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]

missings = set(fruit_types) - set(fruits)
for missing in missings:
    print(fruit_types.index(missing))
输出:
上述差异是有效的。但如果您希望代码只检查两个列表中的一个是否缺少元素(在您的样式中)。您可以使用枚举函数

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]

def find_missing_index():
    for index, element in enumerate(fruit_types):
        if element not in fruits:
            print(fruit_types[index], index)

def main():
    find_missing_index()

if __name__ == '__main__':
    main()
毫无疑问,您想要的是set-difference代码,但是其他示例很多


为了提高性能,最好提前将
水果
转换为
集合

fset = set(fruits)
for index, element in enumerate(fruit_types):
    if element not in fset:
        print(fruit_types[index], index)

这将成员资格测试减少为常量,
O(1)
lookup。

上述设置差异起作用。但如果您希望代码只检查两个列表中的一个是否缺少元素(在您的样式中)。您可以使用枚举函数

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]

def find_missing_index():
    for index, element in enumerate(fruit_types):
        if element not in fruits:
            print(fruit_types[index], index)

def main():
    find_missing_index()

if __name__ == '__main__':
    main()
毫无疑问,您想要的是set-difference代码,但是其他示例很多


为了提高性能,最好提前将
水果
转换为
集合

fset = set(fruits)
for index, element in enumerate(fruit_types):
    if element not in fset:
        print(fruit_types[index], index)
这将成员资格测试减少为常量,
O(1)
查找。

您可以执行以下操作:

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]
missing_elements = []
for element in fruit_types:
    if(element not in fruits):
        missing_elements.append(element)
print(missing_elements)
>>> ["pear"]
创建一个空列表,如果元素不在水果中,则添加到该列表中。

您可以执行以下操作:

fruit_types = ["apple","pear","strawberry","mango"]
fruits = ["apple","strawberry","mango"]
missing_elements = []
for element in fruit_types:
    if(element not in fruits):
        missing_elements.append(element)
print(missing_elements)
>>> ["pear"]


创建一个空列表,如果元素不在水果中,则添加到该列表中。

我想您需要再指定一点提示:如果
if
条件为真,您会怎么做?还记得当你的教授向你解释作业时回到课堂上吗you@cᴏʟᴅsᴘᴇᴇᴅ 如果“If”条件为true,则循环应标识缺失元素所在的索引。OK。好。下一个提示。请看
enumerate
。我想您需要再指定一点提示:如果
条件为真,您会怎么做?还记得当你的教授向你解释作业时回到课堂上吗you@cᴏʟᴅsᴘᴇᴇᴅ 如果“If”条件为true,则循环应标识缺失元素所在的索引。OK。好。下一个提示。看看《枚举》
。我没有投反对票,但这不是最好的答案。当您可以在线性时间内轻松完成此操作时,是否需要继续循环调用
index
?@ReblochonMasque抱歉?这是打印出来的。@cᴏʟᴅsᴘᴇᴇᴅ 我不知道。请您留下评论或编辑我的答案,这样我们就可以学到新的东西了Thanks@cᴏʟᴅsᴘᴇᴇᴅ 索引也包含列表吗?不,它不包含。另外,我只是想对你目前的解决方案留下反馈,没别的。我没有投反对票,但这不是最好的答案。当您可以在线性时间内轻松完成此操作时,是否需要继续循环调用
index
?@ReblochonMasque抱歉?这是打印出来的。@cᴏʟᴅsᴘᴇᴇᴅ 我不知道。请您留下评论或编辑我的答案,这样我们就可以学到新的东西了Thanks@cᴏʟᴅsᴘᴇᴇᴅ 索引也包含列表吗?不,它不包含。另外,我只想留下关于您当前解决方案的反馈,仅此而已。与上面一样,您确实不应该在循环中调用
.index
。在最坏的情况下,这将变成二次型。对于4个元素的列表,二次型是完全可以接受的。对于4个元素,可能是这样,但您不想将这种思维向前传播,对吗?您所指的是“过度杀戮易于出现错误的复杂代码?”与上面一样,您确实不应该在循环中调用
.index
。在最坏的情况下,这将变成二次型。对于4个元素的列表,二次型是完全可以接受的。对于4个元素,可能是这样,但您不想将这种思维向前传播,是吗?您指的是“过度杀戮容易出现bug的复杂代码”是什么?为什么<代码>如果元素不在水果中
设置差异
然后进行
索引
搜索效率更低。谢谢你的快速搜索。)他认为最好在自己的代码中简单地插入枚举函数(以便更好地理解)。但那更好,为什么<代码>如果元素不在水果中比
设置差异
然后进行
索引
搜索效率更低。谢谢你的快速搜索。)他认为最好在自己的代码中简单地插入枚举函数(以便更好地理解)。但那更好。