遍历列表的每个元素 [英] Loop through each element of the list

查看:337
本文介绍了遍历列表的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我正在尝试构建一个函数,该函数将选择列表中的每个元素,将每个元素存储在变量中并执行某些操作(使用循环)

Hi i am new to python and i am trying to build a function which will select each element of the list store each element in a variable and do something (using a loop)

StrLst = ['aaa','bbbaaa','cccbbb','aaabbb']

StrLst = ['aaa','bbbaaa','cccbbb','aaabbb']

LstEle =列表[0]

LstEle = list[0]

使用LstEle变量

然后转到下一个元素,对整个列表执行相同的操作

and then go to the next element and do the same keep doing this for the whole list

推荐答案

您可以像这样使用any函数

>>> any(item in input_string for item in my_list)

如果至少满足一次条件(item in input_string),则

any将返回True.在这种情况下,它循环遍历my_list,并使用in运算符检查当前项是否在input_string中.如果my_list中的任何项目位于input_string中,它将返回True

any will return True if the condition (item in input_string) is satisfied atleast once. In this case, it iterates through my_list and checks if the current item is in the input_string with the in operator. If any of the items in my_list is in the input_string, it will return True

这篇关于遍历列表的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆