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

如何在python中的元素列表中检查空元素,python,Python,例如,我有一个列表,其中包含如下数据: customer1 = ['Dan','24','red'] customer2 = ['Bob',' ','Blue'] customerlist = [customer1, customer2] for c in customerlist: if not in c: ***RUN CODE*** else: print('Customer

例如,我有一个列表,其中包含如下数据:

    customer1 = ['Dan','24','red']
    customer2 = ['Bob',' ','Blue']
    customerlist = [customer1, customer2]
    for c in customerlist:
        if not in c:
            ***RUN CODE***
        else:
            print('Customer Complete')
我想运行一行代码,如果其中一个元素为空,它将运行一个函数。例如,类似这样的事情:

    customer1 = ['Dan','24','red']
    customer2 = ['Bob',' ','Blue']
    customerlist = [customer1, customer2]
    for c in customerlist:
        if not in c:
            ***RUN CODE***
        else:
            print('Customer Complete')
这样,如果客户丢失数据,我可以运行一些代码


谢谢你的帮助

您可以使用中的
检查
'

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

您可以使用
中的
来检查
'

for c in customerlist:
    if ' ' in c:
        RUN CODE
    else:
        print('Customer Complete')
与此相反:

    if not in c:
你想要这个:

    for val in c:
        if not val.strip():
它基本上检查是否有任何字符串是空的(空字符串在Python中是“false”)。剥离首先检测只包含空格的字符串。

而不是此字符串:

    if not in c:
你想要这个:

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

它基本上检查是否有任何字符串是空的(空字符串在Python中是“false”)。剥离首先检测只包含空格的字符串。

Guy和John给出的两个答案都是正确的,但您可能会感兴趣研究一下:

class客户:
def uuu init uuuu(self,name,age=None,color=None):
self.name=名称
self.age=age if age else age\u函数\u生成器()
self.color=color if color else color\u函数\u生成器()
要创建客户,只需执行以下操作:

c1 = Customer(name = "Dan", age = 24, color = "red")
c2 = Customer(name = "Bob", color = "Blue")
c2
的情况下,将调用函数
age\u function\u generator()
(此处未定义)。要访问customer对象的属性,可以执行以下操作:

打印(c1.姓名,c1.年龄,c1.颜色)

盖伊和约翰给出的两个答案都是正确的,但也许你会感兴趣去探究:

class客户:
def uuu init uuuu(self,name,age=None,color=None):
self.name=名称
self.age=age if age else age\u函数\u生成器()
self.color=color if color else color\u函数\u生成器()
要创建客户,只需执行以下操作:

c1 = Customer(name = "Dan", age = 24, color = "red")
c2 = Customer(name = "Bob", color = "Blue")
c2
的情况下,将调用函数
age\u function\u generator()
(此处未定义)。要访问customer对象的属性,可以执行以下操作:

打印(c1.姓名,c1.年龄,c1.颜色)

您可以使用Python正则表达式搜索列表中的空白项。正则表达式是定义模式的字符序列。有关Python正则表达式的更多信息,请访问: 及

请替换以下代码

for c in customerlist:
        if not in c:
使用以下代码:

for i in range(len(customerlist)):
    for j in range(len(customer1)):
        emptylist = re.findall('\s*', customerlist[i][j])
不要忘记在导入Python re模块的代码开头包含“import re”

完整代码:

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
输出:

There are blank entries
在守则中:

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

re.findall()搜索零个或多个空格字符(\s)的实例(*),customerlist是迭代列表。customerlist[i][j],因为它是一个列表列表。

您可以使用Python正则表达式搜索列表中的空白条目。正则表达式是定义模式的字符序列。有关Python正则表达式的更多信息,请访问: 及

请替换以下代码

for c in customerlist:
        if not in c:
使用以下代码:

for i in range(len(customerlist)):
    for j in range(len(customer1)):
        emptylist = re.findall('\s*', customerlist[i][j])
不要忘记在导入Python re模块的代码开头包含“import re”

完整代码:

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
输出:

There are blank entries
在守则中:

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

re.findall()搜索零个或多个空格字符(\s)的实例(*),customerlist是迭代列表。customerlist[i][j]因为它是一个列表列表。

什么是空元素?什么是空元素?这认为
'Bob'
是一个空字符串。@AlexanderCécile:我不这么认为,你看的是错误的洋葱层。@AlexanderCécile不,不是
c
是一个列表,而不是字符串。要展开上面的内容,您也可以这样做:
如果有(x在a中代表x在(“”,,'empty',None)):
检查空索引的多个表示形式。@Guy Right,我看错了列表,对不起。这认为
'Bob'
是一个空字符串。@AlexanderCécile:我不这么认为,“你看错了洋葱的一层。”亚历山大·塞西尔:不,不是
c
是一个列表,而不是字符串。若要展开上面的内容,您还可以执行:
如果有(x in a代表x in(“”,,'empty',None)):
检查空索引的多个表示形式。@Guy Right,我看错了列表,抱歉。