如何通过连续前缀将文件名分组到列表中?

发布于 2025-01-24 13:35:17 字数 598 浏览 2 评论 0原文

我有多个具有相同后缀的文件(_01_020301.txt)和不同的数字前缀。目标是通过连续前缀将它们分组为列表。

这是一个示例:

09456_01_020301.txt
09457_01_020301.txt
09458_01_020301.txt
09459_01_020301.txt
09460_01_020301.txt
09465_01_020301.txt
09466_01_020301.txt
09467_01_020301.txt
09468_01_020301.txt

因为09456〜09460和09465〜09468是两个连续的组,因此结果应为:

[['09456_01_020301.txt',
  '09457_01_020301.txt',
  '09458_01_020301.txt',
  '09459_01_020301.txt',
  '09460_01_020301.txt'],
 ['09465_01_020301.txt',
  '09466_01_020301.txt',
  '09467_01_020301.txt',
  '09468_01_020301.txt']]

I have multiple files which have the same suffix (_01_020301.txt) and different number prefixes. The goal is to group them into lists by the consecutive prefix.

Here's an example:

09456_01_020301.txt
09457_01_020301.txt
09458_01_020301.txt
09459_01_020301.txt
09460_01_020301.txt
09465_01_020301.txt
09466_01_020301.txt
09467_01_020301.txt
09468_01_020301.txt

Because 09456~09460 and 09465~09468 are two consecutive groups, the result should be:

[['09456_01_020301.txt',
  '09457_01_020301.txt',
  '09458_01_020301.txt',
  '09459_01_020301.txt',
  '09460_01_020301.txt'],
 ['09465_01_020301.txt',
  '09466_01_020301.txt',
  '09467_01_020301.txt',
  '09468_01_020301.txt']]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

审判长 2025-01-31 13:35:17

另一个解决方案是,使用 itertools.groupbys.groupbys.groupbys.groupby

from itertools import groupby

lst = [
    "09456_01_020301.txt",
    "09457_01_020301.txt",
    "09458_01_020301.txt",
    "09459_01_020301.txt",
    "09460_01_020301.txt",
    "09465_01_020301.txt",
    "09466_01_020301.txt",
    "09467_01_020301.txt",
    "09468_01_020301.txt",
]

out = []
for _, g in groupby(enumerate(lst), lambda k: int(k[1].split("_")[0]) - k[0]):
    out.append([v for _, v in g])

print(out)

打印:

[
    [
        "09456_01_020301.txt",
        "09457_01_020301.txt",
        "09458_01_020301.txt",
        "09459_01_020301.txt",
        "09460_01_020301.txt",
    ],
    [
        "09465_01_020301.txt",
        "09466_01_020301.txt",
        "09467_01_020301.txt",
        "09468_01_020301.txt",
    ],
]

Another solution, using itertools.groupby:

from itertools import groupby

lst = [
    "09456_01_020301.txt",
    "09457_01_020301.txt",
    "09458_01_020301.txt",
    "09459_01_020301.txt",
    "09460_01_020301.txt",
    "09465_01_020301.txt",
    "09466_01_020301.txt",
    "09467_01_020301.txt",
    "09468_01_020301.txt",
]

out = []
for _, g in groupby(enumerate(lst), lambda k: int(k[1].split("_")[0]) - k[0]):
    out.append([v for _, v in g])

print(out)

Prints:

[
    [
        "09456_01_020301.txt",
        "09457_01_020301.txt",
        "09458_01_020301.txt",
        "09459_01_020301.txt",
        "09460_01_020301.txt",
    ],
    [
        "09465_01_020301.txt",
        "09466_01_020301.txt",
        "09467_01_020301.txt",
        "09468_01_020301.txt",
    ],
]
帅气称霸 2025-01-31 13:35:17

这是使用简单循环的一种方法:

l = ['09456_01_020301.txt',
     '09457_01_020301.txt',
     '09458_01_020301.txt',
     '09459_01_020301.txt',
     '09460_01_020301.txt',
     '09465_01_020301.txt',
     '09466_01_020301.txt',
     '09467_01_020301.txt',
     '09468_01_020301.txt']


out = []
prev = float('-inf')
for i in l:
    prefix = int(i.partition('_')[0])
    if prefix == prev+1:
        out[-1].append(i)
    else:
        out.append([i])
    prev = prefix

输出:

[['09456_01_020301.txt',
  '09457_01_020301.txt',
  '09458_01_020301.txt',
  '09459_01_020301.txt',
  '09460_01_020301.txt'],
 ['09465_01_020301.txt',
  '09466_01_020301.txt',
  '09467_01_020301.txt',
  '09468_01_020301.txt']]

Here is one way using a simple loop:

l = ['09456_01_020301.txt',
     '09457_01_020301.txt',
     '09458_01_020301.txt',
     '09459_01_020301.txt',
     '09460_01_020301.txt',
     '09465_01_020301.txt',
     '09466_01_020301.txt',
     '09467_01_020301.txt',
     '09468_01_020301.txt']


out = []
prev = float('-inf')
for i in l:
    prefix = int(i.partition('_')[0])
    if prefix == prev+1:
        out[-1].append(i)
    else:
        out.append([i])
    prev = prefix

output:

[['09456_01_020301.txt',
  '09457_01_020301.txt',
  '09458_01_020301.txt',
  '09459_01_020301.txt',
  '09460_01_020301.txt'],
 ['09465_01_020301.txt',
  '09466_01_020301.txt',
  '09467_01_020301.txt',
  '09468_01_020301.txt']]
∝单色的世界 2025-01-31 13:35:17

对于输入的假设为str(例如,.csv文件),我通过比较以前的后缀和当前后缀来创建以下代码。

input = """09456_01_020301.txt
09457_01_020301.txt
09458_01_020301.txt
09459_01_020301.txt
09460_01_020301.txt
09465_01_020301.txt
09466_01_020301.txt
09467_01_020301.txt
09468_01_020301.txt"""

result = []  # for the final result
tmp_list = []  # for a temporary sub_list for consecutie suffix files

lines = input.split('\n')
tmp_list.append(lines[0])
prev_suffix = int(lines[0][:5].lstrip('0'))  # lstrip('0') to remove leading zero (for example, 09456 to 9456)

for line in lines[1:]:
    current_suffix = int(line[:5].lstrip('0'))
    if current_suffix == (prev_suffix + 1):  # 9457 == 0946+1 ?
        tmp_list.append(line)
    else:
        result.append(tmp_list)
        tmp_list = [line]
    prev_suffix = current_suffix
result.append(tmp_list)

print(result)
# [['09456_01_020301.txt', '09457_01_020301.txt', '09458_01_020301.txt', '09459_01_020301.txt', '09460_01_020301.txt'], ['09465_01_020301.txt', '09466_01_020301.txt', '09467_01_020301.txt', '09468_01_020301.txt']]

With assumtion of the input is a str (for example, .csv file), I created following code by comparing previous suffix and current suffix.

input = """09456_01_020301.txt
09457_01_020301.txt
09458_01_020301.txt
09459_01_020301.txt
09460_01_020301.txt
09465_01_020301.txt
09466_01_020301.txt
09467_01_020301.txt
09468_01_020301.txt"""

result = []  # for the final result
tmp_list = []  # for a temporary sub_list for consecutie suffix files

lines = input.split('\n')
tmp_list.append(lines[0])
prev_suffix = int(lines[0][:5].lstrip('0'))  # lstrip('0') to remove leading zero (for example, 09456 to 9456)

for line in lines[1:]:
    current_suffix = int(line[:5].lstrip('0'))
    if current_suffix == (prev_suffix + 1):  # 9457 == 0946+1 ?
        tmp_list.append(line)
    else:
        result.append(tmp_list)
        tmp_list = [line]
    prev_suffix = current_suffix
result.append(tmp_list)

print(result)
# [['09456_01_020301.txt', '09457_01_020301.txt', '09458_01_020301.txt', '09459_01_020301.txt', '09460_01_020301.txt'], ['09465_01_020301.txt', '09466_01_020301.txt', '09467_01_020301.txt', '09468_01_020301.txt']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文