Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表元素的Grep_Python_List_Grep - Fatal编程技术网

Python 列表元素的Grep

Python 列表元素的Grep,python,list,grep,Python,List,Grep,我有一个文件名列表: names = ['aet2000','ppt2000', 'aet2001', 'ppt2001'] 虽然我发现了一些可以对字符串进行grep处理的函数,但我还没有弄清楚如何对列表中的所有元素进行grep处理 例如,我想: grep(names,'aet') 并获得: ['aet2000','aet2001'] 当然这不太难,但我对Python还不熟悉 更新 上述问题显然不够准确。下面的所有答案都适用于示例,但不适用于我的实际数据。以下是我创建文件名列表的代码:

我有一个文件名列表:

names = ['aet2000','ppt2000', 'aet2001', 'ppt2001']
虽然我发现了一些可以对字符串进行grep处理的函数,但我还没有弄清楚如何对列表中的所有元素进行grep处理

例如,我想:

grep(names,'aet')
并获得:

['aet2000','aet2001']
当然这不太难,但我对Python还不熟悉


更新 上述问题显然不够准确。下面的所有答案都适用于示例,但不适用于我的实际数据。以下是我创建文件名列表的代码:

years = range(2000,2011)
months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
variables = ["cwd","ppt","aet","pet","tmn","tmx"]     #  *variable name*  with wildcards   
tifnames = list(range(0,(len(years)*len(months)*len(variables)+1)  ))
i = 0
for variable in variables:
   for year in years:
      for month in months:
         fullname = str(variable)+str(year)+str(month)+".tif"
         tifnames[i] = fullname
         i = i+1 
运行筛选器(lambda x:x中的'aet',tifnames)或其他答案返回:

Traceback (most recent call last):
  File "<pyshell#89>", line 1, in <module>
    func(tifnames,'aet')
  File "<pyshell#88>", line 2, in func
    return [i for i in l if s in i]
TypeError: argument of type 'int' is not iterable
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
func(tifnames,'aet')
文件“”,第2行,在func中
返回[i代表i,如果s代表i]
TypeError:类型为“int”的参数不可编辑
尽管tifnames是一个字符串列表:

type(tifnames[1])
<type 'str'>
类型(名称[1])
你们知道这是怎么回事吗?再次感谢

>>> names = ['aet2000', 'ppt2000', 'aet2001', 'ppt2001']
>>> def grep(l, s):
...     return [i for i in l if s in i]
... 
>>> grep(names, 'aet')
['aet2000', 'aet2001']
Regex版本,更接近grep,但在这种情况下不需要:

>>> def func(l, s):
...     return [i for i in l if re.search(s, i)]
... 
>>> func(names, r'aet')
['aet2000', 'aet2001']
使用
过滤器()

使用
regex

>>> import re
>>> filter(lambda x: re.search(r'aet', x), names)
['aet2000', 'aet2001']

在Python3中,filter返回一个迭代器,因此需要对其进行列表调用
list()

>>> list(filter(lambda x:'aet' in x, names))
['aet2000', 'aet2001']
否则使用列表理解(它将在Python 2和Python 3中工作:

>>> [name for name in names if 'aet' in name]
['aet2000', 'aet2001']

您应该试着研究pythong模块。Bellow我有一个使用re的python grep函数实现。它将帮助您了解re是如何工作的(当然,只有在您阅读了re之后)


试试这个。它可能不是所有显示的代码中“最短”的,但是对于试图学习python的人来说,我认为它教的东西更多

names = ['aet2000','ppt2000', 'aet2001', 'ppt2001']
found = []
for name in names:
    if 'aet' in name:
       found.append(name)
print found
输出

['aet2000', 'aet2001']
编辑: 更改为生成列表

另见:


您不需要预先分配列表
tifnames
或使用计数器放入元素。只需将生成的数据附加到列表中或使用列表即可

就这样做吧:

import re

years = ['2000','2011']
months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
variables = ["cwd","ppt","aet","pet","tmn","tmx"]     #  *variable name*  with wildcards   
tifnames = []
for variable in variables:
   for year in years:
      for month in months:
         fullname = variable+year+month+".tif"
         tifnames.append(fullname)

print tifnames
print '==='
print filter(lambda x: re.search(r'aet',x),tifnames)
印刷品:

['cwd2000jan.tif', 'cwd2000feb.tif', 'cwd2000mar.tif', 'cwd2000apr.tif', 'cwd2000may.tif', 'cwd2000jun.tif', 'cwd2000jul.tif', 'cwd2000aug.tif', 'cwd2000sep.tif', 'cwd2000oct.tif', 'cwd2000nov.tif', 'cwd2000dec.tif', 'cwd2011jan.tif', 'cwd2011feb.tif', 'cwd2011mar.tif', 'cwd2011apr.tif', 'cwd2011may.tif', 'cwd2011jun.tif', 'cwd2011jul.tif', 'cwd2011aug.tif', 'cwd2011sep.tif', 'cwd2011oct.tif', 'cwd2011nov.tif', 'cwd2011dec.tif', 'ppt2000jan.tif', 'ppt2000feb.tif', 'ppt2000mar.tif', 'ppt2000apr.tif', 'ppt2000may.tif', 'ppt2000jun.tif', 'ppt2000jul.tif', 'ppt2000aug.tif', 'ppt2000sep.tif', 'ppt2000oct.tif', 'ppt2000nov.tif', 'ppt2000dec.tif', 'ppt2011jan.tif', 'ppt2011feb.tif', 'ppt2011mar.tif', 'ppt2011apr.tif', 'ppt2011may.tif', 'ppt2011jun.tif', 'ppt2011jul.tif', 'ppt2011aug.tif', 'ppt2011sep.tif', 'ppt2011oct.tif', 'ppt2011nov.tif', 'ppt2011dec.tif', 'aet2000jan.tif', 'aet2000feb.tif', 'aet2000mar.tif', 'aet2000apr.tif', 'aet2000may.tif', 'aet2000jun.tif', 'aet2000jul.tif', 'aet2000aug.tif', 'aet2000sep.tif', 'aet2000oct.tif', 'aet2000nov.tif', 'aet2000dec.tif', 'aet2011jan.tif', 'aet2011feb.tif', 'aet2011mar.tif', 'aet2011apr.tif', 'aet2011may.tif', 'aet2011jun.tif', 'aet2011jul.tif', 'aet2011aug.tif', 'aet2011sep.tif', 'aet2011oct.tif', 'aet2011nov.tif', 'aet2011dec.tif', 'pet2000jan.tif', 'pet2000feb.tif', 'pet2000mar.tif', 'pet2000apr.tif', 'pet2000may.tif', 'pet2000jun.tif', 'pet2000jul.tif', 'pet2000aug.tif', 'pet2000sep.tif', 'pet2000oct.tif', 'pet2000nov.tif', 'pet2000dec.tif', 'pet2011jan.tif', 'pet2011feb.tif', 'pet2011mar.tif', 'pet2011apr.tif', 'pet2011may.tif', 'pet2011jun.tif', 'pet2011jul.tif', 'pet2011aug.tif', 'pet2011sep.tif', 'pet2011oct.tif', 'pet2011nov.tif', 'pet2011dec.tif', 'tmn2000jan.tif', 'tmn2000feb.tif', 'tmn2000mar.tif', 'tmn2000apr.tif', 'tmn2000may.tif', 'tmn2000jun.tif', 'tmn2000jul.tif', 'tmn2000aug.tif', 'tmn2000sep.tif', 'tmn2000oct.tif', 'tmn2000nov.tif', 'tmn2000dec.tif', 'tmn2011jan.tif', 'tmn2011feb.tif', 'tmn2011mar.tif', 'tmn2011apr.tif', 'tmn2011may.tif', 'tmn2011jun.tif', 'tmn2011jul.tif', 'tmn2011aug.tif', 'tmn2011sep.tif', 'tmn2011oct.tif', 'tmn2011nov.tif', 'tmn2011dec.tif', 'tmx2000jan.tif', 'tmx2000feb.tif', 'tmx2000mar.tif', 'tmx2000apr.tif', 'tmx2000may.tif', 'tmx2000jun.tif', 'tmx2000jul.tif', 'tmx2000aug.tif', 'tmx2000sep.tif', 'tmx2000oct.tif', 'tmx2000nov.tif', 'tmx2000dec.tif', 'tmx2011jan.tif', 'tmx2011feb.tif', 'tmx2011mar.tif', 'tmx2011apr.tif', 'tmx2011may.tif', 'tmx2011jun.tif', 'tmx2011jul.tif', 'tmx2011aug.tif', 'tmx2011sep.tif', 'tmx2011oct.tif', 'tmx2011nov.tif', 'tmx2011dec.tif']
===
['aet2000jan.tif', 'aet2000feb.tif', 'aet2000mar.tif', 'aet2000apr.tif', 'aet2000may.tif', 'aet2000jun.tif', 'aet2000jul.tif', 'aet2000aug.tif', 'aet2000sep.tif', 'aet2000oct.tif', 'aet2000nov.tif', 'aet2000dec.tif', 'aet2011jan.tif', 'aet2011feb.tif', 'aet2011mar.tif', 'aet2011apr.tif', 'aet2011may.tif', 'aet2011jun.tif', 'aet2011jul.tif', 'aet2011aug.tif', 'aet2011sep.tif', 'aet2011oct.tif', 'aet2011nov.tif', 'aet2011dec.tif']
而且,如果您觉得这更具可读性,那么Python将更惯用:

years = ['2000','2011']
months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
vars = ["cwd","ppt","aet","pet","tmn","tmx"]        
tifnames = [v+y+m+".tif" for y in years for m in months for v in vars]
print tifnames
print '==='
print [e for e in tifnames if re.search(r'aet',e)]

…相同的输出

如果您像这样使用上述过滤器之一:

filter(lambda x:'aet' in x, names)
你会得到这个错误:

TypeError: a bytes-like object is required, not 'str'
您需要对字符串术语进行编码:

filter(lambda x:'aet'.encode() in x, names)

这不会产生OP要求的输出,即列表。哦,谢谢@gerrat。没有实现OP需要的列表输出。修复。请查看
tifnames[-1]
;它是一个整数。只需编写
tifnames=[]
然后
tifnames.append(全名),而不是预先分配空间
,如果索引有问题,也可以使用字典。列表中的最后一项是792(整数),这就是为什么在运行我们的代码时出现错误的原因。非常感谢!!!!!!!!!!!!!!!!!!!!!!!救生员所有人都+1进行列表操作。@mmann1123您是否有c语言背景?表达式
list(范围(0),(len(年)*len(月)*len(变量)+1))
既错误也不需要。错误是因为末尾的+1;列表将是一个太长的元素。不需要它,因为您应该只使用
tifnames=[]
然后将值附加到循环中的值。值得注意的是,lambda arg声明和python3中的函数定义(例如:
lambda x:aet in x
)之间可能会有一个空格,您可能希望将过滤器包装在一个类似以下的列表中:list(filter(lambda x:aet in x,names))如果您想要一个列表,列表理解也可以工作:[n代表n,如果n中的'aet'代表n]。列表理解确实是您想要的,并且是Python的Perl grep。内置函数filter()如果要动态分析数据,请返回一个迭代器。python3示例?@KhurshidAlam Updated。这更适合作为注释,因为它不是一个完整、自包含的答案。
TypeError: a bytes-like object is required, not 'str'
filter(lambda x:'aet'.encode() in x, names)