Python: Group a list of file names according to common name identifier

Dipole

In a directory I have some files:

temperature_Resu05_les_spec_r0.0300.0
temperature_Resu05_les_spec_r0.0350.0
temperature_Resu05_les_spec_r0.0400.0
temperature_Resu05_les_spec_r0.0450.0
temperature_Resu06_les_spec_r0.0300.0
temperature_Resu06_les_spec_r0.0350.0
temperature_Resu06_les_spec_r0.0400.0
temperature_Resu06_les_spec_r0.0450.0
temperature_Resu07_les_spec_r0.0300.0
temperature_Resu07_les_spec_r0.0350.0
temperature_Resu07_les_spec_r0.0400.0
temperature_Resu07_les_spec_r0.0450.0
temperature_Resu08_les_spec_r0.0300.0
temperature_Resu08_les_spec_r0.0350.0
temperature_Resu08_les_spec_r0.0400.0
temperature_Resu08_les_spec_r0.0450.0
temperature_Resu09_les_spec_r0.0300.0
temperature_Resu09_les_spec_r0.0350.0
temperature_Resu09_les_spec_r0.0400.0
temperature_Resu09_les_spec_r0.0450.0

I need a list of all the files that have the same identifier XXXX as in _rXXXX. For example one such list would be composed of

temperature_Resu05_les_spec_r0.0300.0
temperature_Resu06_les_spec_r0.0300.0
temperature_Resu07_les_spec_r0.0300.0
temperature_Resu08_les_spec_r0.0300.0
temperature_Resu09_les_spec_r0.0300.0

I don't know a priori what the XXXX values are going to be so I can't iterate through them and match like that. Im thinking this might best be handles with a regular expression. Any ideas?

Padraic Cunningham

No a regex is not the best way, you pattern is very straight forward, just str.rsplit on the _r and use the right element of the split as the key to group the data with. A defaultdict will do the grouping efficiently:

from collections import defaultdict

with open("yourfile") as f:
    groups = defaultdict(list)
    for line in f:
        groups[line.rsplit("_r",1)[1]].append(line.rstrip())

from pprint import pprint as pp

pp(groups.values())

Which for your sample will give you:

[['temperature_Resu09_les_spec_r0.0450.0'],
 ['temperature_Resu05_les_spec_r0.0300.0',
  'temperature_Resu06_les_spec_r0.0300.0',
  'temperature_Resu07_les_spec_r0.0300.0',
  'temperature_Resu08_les_spec_r0.0300.0',
  'temperature_Resu09_les_spec_r0.0300.0'],
 ['temperature_Resu05_les_spec_r0.0400.0',
  'temperature_Resu06_les_spec_r0.0400.0',
  'temperature_Resu07_les_spec_r0.0400.0',
  'temperature_Resu08_les_spec_r0.0400.0',
  'temperature_Resu09_les_spec_r0.0400.0'],
 ['temperature_Resu05_les_spec_r0.0450.0',
  'temperature_Resu06_les_spec_r0.0450.0',
  'temperature_Resu07_les_spec_r0.0450.0',
  'temperature_Resu08_les_spec_r0.0450.0'],
 ['temperature_Resu05_les_spec_r0.0350.0',
  'temperature_Resu06_les_spec_r0.0350.0',
  'temperature_Resu07_les_spec_r0.0350.0',
  'temperature_Resu08_les_spec_r0.0350.0',
  'temperature_Resu09_les_spec_r0.0350.0']]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sort a list of names imported from txt file by last name in Python

From Dev

Python libtorrent, get file list names

From Dev

Moving oldest file names with different extensions to common folder. Python

From Dev

How can I sort a list of file names by some substring of the name?

From Dev

How can I sort a list of file names by some substring of the name?

From Dev

How to rename files according to names in a text file?

From Dev

Python, string slicing (getting file names from a list of file locations)

From Dev

Python: how to search for specific "string" in directory name (not individual file names)

From Dev

element as the list names and list name as the element in a list?

From Dev

collapsing graphs according to a common parameter in iGraph [Python]

From Dev

Name worksheets based on a list of Names

From Dev

List names as JSON object name

From Dev

3ds maxscript group objects according to name and name + suffix

From Dev

Perl Script to sort a list of names, File name passed from a C script and then sorted list passed back

From Dev

Need to List in sorted according to name in java

From Dev

Need to List in sorted according to name in java

From Dev

Bat file list file names

From Dev

File Revision List File Names?

From Dev

Get a list of file names from HDFS using python

From Dev

Extract substring from list of file names in Python or R

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

Python: List directories/sub-directories with file names

From Dev

Searching in a big list file for names using in operator in python

From Dev

Extract substring from list of file names in Python or R

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

how to pass file names to list in python in a certain format?

From Dev

How do I use a part of a file name in python as a unique identifier on which to base a join?

From Dev

Python: Subsetting a list according to the attribute

From Dev

Sorting names entered by the user in alphabetical order according to the last name

Related Related

  1. 1

    How to sort a list of names imported from txt file by last name in Python

  2. 2

    Python libtorrent, get file list names

  3. 3

    Moving oldest file names with different extensions to common folder. Python

  4. 4

    How can I sort a list of file names by some substring of the name?

  5. 5

    How can I sort a list of file names by some substring of the name?

  6. 6

    How to rename files according to names in a text file?

  7. 7

    Python, string slicing (getting file names from a list of file locations)

  8. 8

    Python: how to search for specific "string" in directory name (not individual file names)

  9. 9

    element as the list names and list name as the element in a list?

  10. 10

    collapsing graphs according to a common parameter in iGraph [Python]

  11. 11

    Name worksheets based on a list of Names

  12. 12

    List names as JSON object name

  13. 13

    3ds maxscript group objects according to name and name + suffix

  14. 14

    Perl Script to sort a list of names, File name passed from a C script and then sorted list passed back

  15. 15

    Need to List in sorted according to name in java

  16. 16

    Need to List in sorted according to name in java

  17. 17

    Bat file list file names

  18. 18

    File Revision List File Names?

  19. 19

    Get a list of file names from HDFS using python

  20. 20

    Extract substring from list of file names in Python or R

  21. 21

    list file names from a folder to a tkinter window, with python 3

  22. 22

    Python: List directories/sub-directories with file names

  23. 23

    Searching in a big list file for names using in operator in python

  24. 24

    Extract substring from list of file names in Python or R

  25. 25

    list file names from a folder to a tkinter window, with python 3

  26. 26

    how to pass file names to list in python in a certain format?

  27. 27

    How do I use a part of a file name in python as a unique identifier on which to base a join?

  28. 28

    Python: Subsetting a list according to the attribute

  29. 29

    Sorting names entered by the user in alphabetical order according to the last name

HotTag

Archive