如何使用python3在某些条件下读取列表?

2024-03-28 22:13:31 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python3在某些条件下读取列表

默认情况下,我想检查第一个列表的日期条件,第二个作为alpha numberic条件,其余所有列表作为numbers条件。在嵌套列表或字典中检查条件容易吗??? 进口re

  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")

Expected Output:

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

Tags: inre列表forstringif条件list
1条回答
网友
1楼 · 发布于 2024-03-28 22:13:31

是的,你可以这样做:

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")

相关问题 更多 >