如何使用python3在某些条件下读取列表?
默认情况下,我想检查第一个具有日期条件的列表,第二个作为字母数字条件,其余所有列表都作为数字条件。在嵌套列表或字典中检查条件是否容易? 汇入
list_1=[["01/01/2019","02/02/2019"],["abc012","def345"],["1","2"],["2.50","3.15"],["4.50","5.55"]]
for i,string in enumerate(list_1):
for j in string:
if re.findall(r"\d{1,2}/\d{1,2}/\d{4}", j):
print(j,"first")
for k in string:
if re.findall(r"[a-zA-Z0-9]", k):
print(k,"second")
for l in string:
if re.findall(r"[0-9]", l):
print(l,"third")
预期输出:
01/01/2019,first
02/02/2019,first
abc012 second
def345 second
1 third
2 third
2.50 third
3.15 third
4.50 third
5.55 third
答案 0 :(得分:1)
是的,您可以像下面这样:
for i,string in enumerate(list_1):
if i == 0 :
for j in string:
if re.findall(r"\d{1,2}/\d{1,2}/\d{4}", j):
print(j,"first")
elif i == 1:
for k in string:
if re.findall(r"[a-zA-Z0-9]", k):
print(k,"second")
else:
for l in string:
if re.findall(r"[0-9]", l):
print(l,"third")