简体   繁体   English

如何检查python中元素列表中的空元素

[英]How to check for a empty element within a list of elements in python

Say for example i have a list of lists that contain data like this:举例来说,我有一个包含如下数据的列表列表:

    customer1 = ['Dan','24','red']
    customer2 = ['Bob',' ','Blue']
    customerlist = [customer1, customer2]

I would like to run a line of code that will run a function if one of these elements is empty.如果这些元素之一为空,我想运行一行代码来运行一个函数。 For example something like this:例如这样的事情:

    for c in customerlist:
        if not in c:
            ***RUN CODE***
        else:
            print('Customer Complete')

That way if a customer is missing data i can run some code.这样,如果客户缺少数据,我可以运行一些代码。

Thanks for the help!谢谢您的帮助!

You can use in to check for ' '您可以使用in检查' '

for c in customerlist:
    if ' ' in c:
        RUN CODE
    else:
        print('Customer Complete')

Instead of this:取而代之的是:

    if not in c:

You want this:你要这个:

    for val in c:
        if not val.strip():

Which basically checks if any of the strings is empty (empty strings are "falsey" in Python).它基本上检查是否有任何字符串为空(空字符串在 Python 中是“falsey”)。 Stripping first detects strings which only contain whitespace.剥离首先检测仅包含空格的字符串。

Both of the answers given by Guy and John are correct, but perhaps it would interest you to look into objects : Guy 和 John 给出的两个答案都是正确的,但也许您会对研究对象感兴趣:

class Customer:
    def __init__(self, name, age = None, color = None):
        self.name = name
        self.age = age if age else age_function_generator()
        self.color = color if color else color_function_generator()

To create a customer, then, simply do:要创建客户,只需执行以下操作:

c1 = Customer(name = "Dan", age = 24, color = "red")
c2 = Customer(name = "Bob", color = "Blue")

In the case of c2 the function age_function_generator() (not defined here) would be called.c2的情况下,将调用函数age_function_generator() (此处未定义)。 To access the attributes of the customer object one would do:要访问客户对象的属性,可以这样做:

print(c1.name, c1.age, c1.color)

You may use Python Regular Expression to search for blank entries on the list.您可以使用 Python 正则表达式来搜索列表中的空白条目。 A Regular Expression is a sequence of characters that define a pattern.正则表达式是定义模式的字符序列。 For more information on Python Regular Expression, kindly visit: w3school link and Google Developer link有关 Python 正则表达式的更多信息,请访问: w3school 链接Google Developer 链接

Kindly replace the following code请替换以下代码

for c in customerlist:
        if not in c:

with the following code:使用以下代码:

for i in range(len(customerlist)):
    for j in range(len(customer1)):
        emptylist = re.findall('\s*', customerlist[i][j])

Dont forget to include 'import re' at the beginning of code to import Python re module不要忘记在代码开头包含 'import re' 以导入 Python re 模块

The complete code:完整代码:

import re
customer1 = ['Dan','24','red']
customer2 = ['Bob',' ','Blue', ' ']
customerlist = [customer1, customer2]

for i in range(len(customerlist)):
    for j in range(len(customer1)):
        emptylist = re.findall('\s*', customerlist[i][j])
if(len(emptylist) == 0):
    print('There are no blank entries')
else:
    print('There are blank entries')
    #code goes here to do something

The output:输出:

There are blank entries

In the code:在代码中:

emptylist = re.findall('\s*', customerlist[i][j])

re.findall() search for zero or more instances(*) of white space character(\\s) with customerlist being the iterating list. re.findall() 搜索零个或多个空白字符 (\\s) 实例 (*),以 customerlist 为迭代列表。 customerlist[i][j] as it is a list of lists. customerlist[i][j] 因为它是一个列表列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM