简体   繁体   English

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

[英]How to read list with some conditions using python3?

How to read list with some conditions using python3? 如何在某些条件下使用python3读取列表?

By default i want to check the first list with date condition and second as alpha numberic condition and rest all list as numbers conditions. 默认情况下,我想检查第一个带有日期条件的列表,第二个作为字母数字条件,其余所有列表都作为数字条件。 Is it easy to check the condition in nested list or dictionary ??? 在嵌套列表或字典中检查条件是否容易? import 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

Yes, you can do this as bellow: 是的,您可以像下面这样进行操作:

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

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

相关问题 如何在python3中读取和列出tgz文件? - How to read and list a tgz file in python3? Python3,如何检查列表中是否有一些字典值? - Python3, how to check whether some dictionary value in list? python3中,子线程满足某些条件时如何暂停和唤醒主线程? - In python3, how to pause and wake up the main thread when some conditions are satisfied by child threads? 如何读取.rtf文件并转换为python3字符串并可以存储在python3列表中? - How to read .rtf file and convert into python3 strings and can be stored in python3 list? python - 使用循环满足某些条件后,如何在python中查询某个列表元素? - How do I query a certain list element in python after they meet some conditions using a loop? 如何从Python列表中提取与某些条件匹配的值? - How to extract values which is match some conditions from list in Python? 如何根据某些条件迭代 python 中的列表项? - How to iterate over the list items in python based on some conditions? 如何使用两个条件在 Python 中对列表进行排序? - How to sort a list in Python using two conditions? 如何使用python3替换列表中的元素? - How to replace a element in list of list using python3? 如何使用Python3打开和读取pdf(最初为.html)文件 - How open and read pdf (originally .html) file using Python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM