Python:如何查找文件列表中最常见的元素

时间:2013-01-31 15:13:17

标签: python optimization coding-style

首先抱歉这个简单的问题,但我无法找出解决问题的最简单方法。

我有一个包含多个不同文件但有共同元素(values_25,_26,_28等)的目录:

xxxxx_25.txt
xxxxx_26.txt
xxxxx_27.txt
xxxxx_28.txt
yyyyy_25.txt
yyyyy_26.txt
yyyyy_27.txt
yyyyy_29.txt
mmmmm_25.txt
mmmmm_26.txt
mmmmm_27.txt
mmmmm_30.txt

我希望将列表作为

xxxxx_25.txt
yyyyy_25.txt
mmmmm_25.txt

xxxxx_26.txt
yyyyy_26.txt
mmmmm_26.txt

xxxxx_27.txt
yyyyy_27.txt
mmmmm_27.txt

xxxxx_28.txt

yyyyy_29.txt

mmmmm_30.txt

4 个答案:

答案 0 :(得分:3)

import re

list_with_file_names = 'xxxx_25.txt xxxxx_26.txt xxxxx_27.txt xxxxx_28.txt yyyyy_25.txt yyyyy_26.txt yyyyy_27.txt yyyyy_29.txt mmmmm_25.txt mmmmm_26.txt mmmmm_27.txt mmmmm_30.txt'.split()

def get_number_and_prefix(text):
    g = re.match('.*(\S+)(\d+)', text)
    return tuple([
        int(g.group(2)),
        g.group(1)])

nice_list = sorted(list_with_file_names, key=get_number_and_prefix)

get_number_and_prefix返回的元组将首先按数字排序,然后按前缀排序

相反,如果您想根据文件名中的数字进行分组,可以使用以下内容:

def update_dict_with_file(dict_, filename):
    g = re.match('.*(\d+)', filename)
    key = g.group(1)
    t = dict_.setdefault(key,[])
    t.append(filename)

mydict = {}
[update_dict_with_file(mydict, filename) 
 for filename in list_with_file_names]

mydict现在包含来自文件名的数字作为键,以及带有文件名作为值的列表

修改

总结到目前为止的所有答案,您只需要使用一个关键的getter函数从列表中构建一个sorted列表,该函数可以从文件名中提取您想要的任何内容。您可以通过带有itertools +列表理解的花哨的单行或者更长的for循环(任何地方都没有yield来完成)。但是,基本上,它们都是一样的。没有火箭科学。

答案 1 :(得分:2)

这样做:

list_of_files = [
    'xxxxx_25.txt',
    'xxxxx_26.txt',
    'xxxxx_27.txt',
    'xxxxx_28.txt',
    'yyyyy_25.txt',
    'yyyyy_26.txt',
    'yyyyy_27.txt',
    'yyyyy_29.txt',
    'mmmmm_25.txt',
    'mmmmm_26.txt',
    'mmmmm_27.txt',
    'mmmmm_30.txt',
    ]

import re
regex = re.compile('_([0-9]+)\.txt$')

def keyfn(name):
    match = regex.search(name)
    if match is None:
        return None
    else:
        return match.group(1)

import itertools
for (key, group) in itertools.groupby(sorted(list_of_files,key=keyfn),keyfn):
    print [x for x in group]

或者如果您想要一个列表列表,请将for循环替换为:

[x for g in itertools.groupby(sorted(list_of_files,key=keyfn),keyfn) for x in g[1]]

答案 2 :(得分:2)

#Considering your list of files is as follows
ur_file_list = """xxxxx_25.txt
xxxxx_26.txt
xxxxx_27.txt
xxxxx_28.txt
yyyyy_25.txt
yyyyy_26.txt
yyyyy_27.txt
yyyyy_29.txt
mmmmm_25.txt
mmmmm_26.txt
mmmmm_27.txt
mmmmm_30.txt"""
#Based on the pattern, you can get the key assuming, you need the part in the
#filename (without ext) after underscore. So this will give you the part without regex
key = lambda e: os.path.splitext(e)[0].split("_")[-1]
from itertools import groupby
#On a sorted list, group on the above key function
#And generate a list of these groups
[list(group) for _, group in groupby(sorted(ur_file_list.splitlines(), key = key), key = key)]
[['xxxxx_25.txt', 'yyyyy_25.txt', 'mmmmm_25.txt'], ['xxxxx_26.txt', 'yyyyy_26.txt', 'mmmmm_26.txt'], ['xxxxx_27.txt', 'yyyyy_27.txt', 'mmmmm_27.txt'], ['xxxxx_28.txt'], ['yyyyy_29.txt'], ['mmmmm_30.txt']]

答案 3 :(得分:1)

collections.defaultdict的使用对于此任务非常方便。

In [1]: import re; from collections import defaultdict

In [2]: filenames
Out[2]: 
['xxxxx_25.txt',
 'xxxxx_26.txt',
 'xxxxx_27.txt',
 'xxxxx_28.txt',
 'yyyyy_25.txt',
 'yyyyy_26.txt',
 'yyyyy_27.txt',
 'yyyyy_29.txt',
 'mmmmm_25.txt',
 'mmmmm_26.txt',
 'mmmmm_27.txt',
 'mmmmm_30.txt']

In [3]: d = defaultdict(list)

In [4]: for filename in filenames:
  ....:     m = re.search(r'_(\d+)\.txt$', filename)
  ....:     if m:
  ....:         d[m.group(1)].append(filename)

In [5]: [sorted(filename_list) for filename_list in d.values()]
Out[5]: 
[['xxxxx_25.txt', 'yyyyy_25.txt'],
 ['mmmmm_26.txt', 'xxxxx_26.txt', 'yyyyy_26.txt'],
 ['mmmmm_27.txt', 'yyyyy_27.txt'],
 ['xxxxx_28.txt'],
 ['yyyyy_29.txt'],
 ['mmmmm_30.txt']]