|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +def print_files(filenames): |
| 4 | + # Set up the loop iteration instructions |
| 5 | + for name in filenames: |
| 6 | + # Use pathlib.Path to print out each file |
| 7 | + print(Path(name).read_text()) |
| 8 | + |
| 9 | +def list_files(filenames): |
| 10 | + # Use pathlib.Path to read the contents of each file |
| 11 | + return [Path(name).read_text() |
| 12 | + # Obtain each name from the list of filenames |
| 13 | + for name in filenames] |
| 14 | + |
| 15 | +filenames = "diabetes.txt", "boston.txt", "digits.txt", "iris.txt", "wine.txt" |
| 16 | +print_files(filenames) |
| 17 | +pprint(list_files(filenames)) |
| 18 | + |
| 19 | + |
| 20 | +def get_matches(filename, query): |
| 21 | + # Filter the list comprehension using an if clause |
| 22 | + return [line for line in Path(filename).open() if query in line] |
| 23 | + |
| 24 | +# Iterate over files to find all matching lines |
| 25 | +matches = [get_matches(name, "Number of") for name in filenames] |
| 26 | +pprint(matches) |
| 27 | + |
| 28 | + |
| 29 | +def flatten(nested_list): |
| 30 | + return (item |
| 31 | + # Obtain each list from the list of lists |
| 32 | + for sublist in nested_list |
| 33 | + # Obtain each element from each individual list |
| 34 | + for item in sublist) |
| 35 | + |
| 36 | +number_generator = (int(substring) for string in flatten(matches) |
| 37 | + for substring in string.split() if substring.isdigit()) |
| 38 | + |
| 39 | +pprint(dict(zip(filenames, zip(number_generator, number_generator)))) |
| 40 | + |
| 41 | + |
| 42 | + |
0 commit comments