任何人都知道为什么我的代码只能找到列表中第一项的文件而不是其他项目?即它将过滤文件“*a.LOG.bz2”在文件名中
l='1001'
dts_lst=list(pd.date_range(datetime.strptime('2022-03-15', '%Y-%m-%d'), Dt_now, freq='D').strftime('%Y-%m-%d')) # list of days
p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l))
to_search=['*a.LOG.bz2', '*b.LOG.bz2', '*c.LOG.bz2' ]
for i in to_search:
f1=map(lambda x, y:Path(x).rglob(y), p2, repeat(i))
for i2 in f1:
print(f"this:::{i2}")
l='1001'
dts_lst=list(pd.date_range(datetime.strptime('2022-03-15', '%Y-%m-%d'), Dt_now, freq='D').strftime('%Y-%m-%d')) # list of days
p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l))
to_search=['*a.LOG.bz2', '*b.LOG.bz2', '*c.LOG.bz2' ]
for i in to_search:
f1=map(lambda x, y:Path(x).rglob(y), p2, repeat(i))
for i2 in f1:
print(f"this:::{i2}")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
p2
应该是p1
,或者是类似于p1
的map
对象,那么我看到你的问题。它以这一行开头:这将创建一个
map
对象,它是一种迭代器。一旦构建了其中一个迭代器,您就可以像任何其他迭代器一样访问它,并且它会为您提供一系列值,直到它到达它要提供的任何序列的末尾。您的问题是您正在迭代此
map
对象三次。你不能那样做。第一次使用迭代器时,您会耗尽它的值。第一次之后,迭代器实际上位于其序列的末尾,因此继续查询它的值将导致不返回任何值。修复代码的最简单方法是从分配给
p1
的map
对象创建一个列表:您可以迭代
list
多次,所以效果很好。另一种选择是将p1
的计算放入循环中,以便每次通过循环都构建一个新的map
对象:顺便说一句...您不需要这里需要
repeat()
迭代器。您可以让lambda
函数采用单个参数,然后直接引用您传递给repeat()
的值:Assuming that
p2
is supposed to bep1
, or is otherwise amap
object similar top1
, then I see your problem. It starts with this line:This creates a
map
object, which is a kind of iterator. Once you've built one of these, you access it like any other iterator, and it gives you a series of values until it reaches the end of whatever sequence it is meant to provide.Your problem is that you are iterating over this
map
object three times. You can't do that. The first time you use the iterator, you exhaust it of values. After the first time, the iterator is effectively at the end of its sequence, and so continuing to query it for values will result in no values being returned.The simplest way to fix your code is to create a list from the
map
object that you are assigning top1
:You can iterate over a
list
multiple times, so this works fine. Another option would be to put the calculation ofp1
inside your loop so that you build a newmap
object each time through your loop:An aside...You don't need the
repeat()
iterators here. You can just let yourlambda
functions take a single parameter, and then refer directly to the value you were passing torepeat()
: