Python 列表中某项的长度(len)仅为2个字母

Python 列表中某项的长度(len)仅为2个字母,python,list,Python,List,我需要从列表中找到项目,但它只有两个字母 userlist = "fox,mi,cottage,tomatoes,bob,bo,fox" userlist = list(userlist.split(",")) 我正在使用Python3.8,我刚刚开始 帮助我:)您的用户列表将包含表示您刚刚分离的stings的元素。 在Python中,您可以通过len(string\u you\u want\u to\u check)检查sting的长度。所以只要检查一

我需要从列表中找到项目,但它只有两个字母

userlist = "fox,mi,cottage,tomatoes,bob,bo,fox"
userlist = list(userlist.split(","))
我正在使用Python3.8,我刚刚开始
帮助我:)

您的用户列表将包含表示您刚刚分离的stings的元素。 在Python中,您可以通过
len(string\u you\u want\u to\u check)
检查sting的长度。所以只要检查一下长度

for element in userlist:
    if len(element)==2:
        print(element)
split()
为您提供了一个列表,因此您可以迭代列表中的元素并筛选具有所需长度的元素:

templist = []
for user in userlist:
    if(len(user) == 2):
        templist.append(user)  # remember to not modify the same list while iterating over it

userlist = templist
可以使用列表理解在一行中完成:

userlist = [elt for elt in userlist.split(",") if len(elt) == 2]
我会:

  userlist = "fox,mi,cottage,tomatoes,bob,bo,fox" 
  userlist = list(userlist.split(",")) 
  for i in userlist:
      if len(i)==2:
          print i

您的意思是
列表(筛选器(lambda项:len(项)==2,userlist.split(“,”))
?不要使用筛选器,而是使用列表comp