在Python中查找元素的列表枚举

在Python中查找元素的列表枚举,python,list,enumeration,Python,List,Enumeration,在python中,如何迭代元素列表并查找列表中是否存在我所需的元素 如果在列表中找到其中任何一个元素,则打印,否则表示未找到任何元素在末尾断言我的代码 我只想从列表中搜索并查找“名称”、“年份”和“城市id”这三个必填元素 最后使用foundcount断言,以便测试失败或通过 这是我的密码: list = [ 'Name, 'Year', 'City_id', 'Region_Id' , 'Location', 'Source', 'Time'] reqVal = [ 'Name', 'Yea

在python中,如何迭代元素列表并查找列表中是否存在我所需的元素

如果在列表中找到其中任何一个元素,则打印,否则表示未找到任何元素在末尾断言我的代码

我只想从列表中搜索并查找“名称”、“年份”和“城市id”这三个必填元素

最后使用foundcount断言,以便测试失败或通过

这是我的密码:

list = [ 'Name, 'Year', 'City_id', 'Region_Id' , 'Location', 'Source', 'Time']

reqVal = [ 'Name', 'Year', 'City_id' ] # items in List - if present or not
foundCount = 0

def isValInList():
    for reqVal in enumerate(list):
        if reqVal in list :
            print("Yes, %s required item is found in List : ", reqVal)
            foundCount += 1
        else:
            print('No required items are found in list')
            break


        assert (foundCount == 0)

isValInList()

所以现在当我运行这段代码时,我得到

No required items are found in list
这显然是错误的,你能建议并纠正我哪里做错了吗。谢谢。

您可以试试:

my_list = ['Name', 'Year', 'City_id', 'Region_Id' , 'Location', 'Source', 'Time']
reqVal = ['Name', 'Year', 'City_id']

count_for_reqVal = {req_val : my_list.count(req_val) for req_val in reqVal}

print(count_for_reqVal)

#output : {'Name': 1, 'Year': 1, 'City_id': 1}
此外,我还试图重用您的代码:

list = [ 'Name', 'Year', 'City_id', 'Region_Id' , 'Location', 'Source', 'Time']

reqVal = [ 'Name', 'Year', 'City_id', 'Test' ] # items in List - if present or not


def isValInList():
    foundCount = 0

    for val in reqVal:
        if val in list :
            print("Yes, '%s' required item is found in list" %  val)
            foundCount += 1

        else:
            print("No, '%s' required item is not in list" %  val)

    if foundCount == 0:
            print('No required items are found in list')



isValInList()

# output:
# Yes, 'Name' required item is found in list
# Yes, 'Year' required item is found in list
# Yes, 'City_id' required item is found in list
# No, 'Test' required item is not in list

你的问题不太容易理解。请说得更具体一些。我理解你的问题,所以写了答案。 列表=['名称'、'年份'、'城市id'、'地区id'、'位置'、'来源'、'时间']

reqVal = [ 'Name', 'Year', 'City_id' ] # items in List - if present or not
foundCount = 0

if 'Name' in list and 'Year' in list 'City_id' in list:
    print('All are present')
使用集

发生了很多事情: -不要使用预定义的名称(如列表)作为变量名 -您需要将变量传递给函数 -你的断言有什么好处? -foundCount必须声明为全局 -枚举生成一个元组 -休息有什么好处

这项工作:

alist = ['Name', 'Year', 'City_id', 'Region_Id' , 'Location', 'Source', 'Time']

reqVal = ['Name', 'Year', 'City_id' ] # items in List - if present or not

def isValInList(alist, blist):
    global foundCount
    for num, entry_a in enumerate(alist):
        if entry_a in blist :
            print("Yes, %s required item is found in List : ", entry_a)
            foundCount += 1
        else:
            print('No required items are found in list')

isValInList(alist, reqVal)

如果我们想将列表转换为元组的iterable列表或基于条件检查获取索引,例如,在线性搜索中,您可能需要保存最小元素的索引,您可以使用枚举函数

例如:

# Python3 code to iterate over a list 
list = [1, 3, 5, 7, 9] 

# Using enumerate() 
for i, val in enumerate(list): 
    print (i, ",",val) 

注意:即使方法2也可以用来查找索引,但是方法1不能,除非每次迭代都增加一个额外的变量,并且方法5给出了该索引的简明表示。

您在迭代错误的列表:

for reqVal in enumerate(list):
    if reqVal in list :
尝试:

这样就不需要枚举了。 我不认为在迭代所有项目之前中断循环有什么意义


此外,最好不要使用list作为var名称,因为这是python中的一个关键词。

感谢大家抽出时间回答我的问题。我个人喜欢Rusu和Carsten的解决方案,因为他们遇到了我的大部分问题。感谢所有响应者。
for reqVal in enumerate(list):
    if reqVal in list :
for item in reqVal:
    if item in list:
        print("Yes, %s required item is found in List : ", element)
        foundCount += 1

    else:
        print('No required items are found in list')