任何人都知道为什么我的代码只能找到列表中第一项的文件而不是其他项目?即它将过滤文件“*a.LOG.bz2”在文件名中

发布于 2025-01-15 11:36:39 字数 437 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-01-22 11:36:39

假设 p2 应该是 p1,或者是类似于 p1map 对象,那么我看到你的问题。它以这一行开头:

p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l))

这将创建一个 map 对象,它是一种迭代器。一旦构建了其中一个迭代器,您就可以像任何其他迭代器一样访问它,并且它会为您提供一系列值,直到它到达它要提供的任何序列的末尾。

您的问题是您正在迭代此 map 对象三次。你不能那样做。第一次使用迭代器时,您会耗尽它的值。第一次之后,迭代器实际上位于其序列的末尾,因此继续查询它的值将导致不返回任何值。

修复代码的最简单方法是从分配给 p1map 对象创建一个列表:

p1=list(map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l)))

您可以迭代 list多次,所以效果很好。另一种选择是将 p1 的计算放入循环中,以便每次通过循环都构建一个新的 map 对象:

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
    
to_search=['*a.LOG.bz2', '*b.LOG.bz2', '*c.LOG.bz2' ]

for i in to_search:
    p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l))
    p2 = p1  # ????
    f1=map(lambda x, y:Path(x).rglob(y), p2, repeat(i))
    for i2 in f1:
        print(f"this:::{i2}")

顺便说一句...您不需要这里需要 repeat() 迭代器。您可以让 lambda 函数采用单个参数,然后直接引用您传递给 repeat() 的值:

p1=map(lambda x:Path(drive / x / 'foldera' / l / 'folderb' ), dts_lst)

f1=map(lambda x:Path(x).rglob(i), p2)

Assuming that p2 is supposed to be p1, or is otherwise a map object similar to p1, then I see your problem. It starts with this line:

p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l))

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 to p1:

p1=list(map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l)))

You can iterate over a list multiple times, so this works fine. Another option would be to put the calculation of p1 inside your loop so that you build a new map object each time through your loop:

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
    
to_search=['*a.LOG.bz2', '*b.LOG.bz2', '*c.LOG.bz2' ]

for i in to_search:
    p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l))
    p2 = p1  # ????
    f1=map(lambda x, y:Path(x).rglob(y), p2, repeat(i))
    for i2 in f1:
        print(f"this:::{i2}")

An aside...You don't need the repeat() iterators here. You can just let your lambda functions take a single parameter, and then refer directly to the value you were passing to repeat():

p1=map(lambda x:Path(drive / x / 'foldera' / l / 'folderb' ), dts_lst)

f1=map(lambda x:Path(x).rglob(i), p2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文