Python - find an expression in a list filled with strings
本问题已经有最佳答案,请猛点这里访问。
我想在一个用字符串填充的列表中找到一个特定条目。例如,我想查找包含数字".0130"的所有字符串(如下面的第三行)
文件如下:
1 2 3 4 | https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/001/MYD021KM.A2018001.0140.006.2018002030901.hdf https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/001/MYD021KM.A2018001.0135.006.2018002030858.hdf https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/001/MYD021KM.A2018001.0130.006.2018002030850.hdf https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/001/MYD021KM.A2018001.0125.006.2018002030903.hdf |
我是这样读文件的:
1 2 3 4 5 | data_links = [] f=open(workdir1+"/download.txt","r") for i in f: data_links.append(i.rstrip()) f.close() |
如何查找包含".0130"部分的所有条目?
1 | my_list = [line for line in file if".0130" in line] |
也可以使用regex
1 2 | import re [i for i in your_list if(re.search('\.0130\.',i) is not None)] |