Skip to content

Commit b4a74e0

Browse files
committed
datacamp creating robust workflows
1 parent 3a3a0ea commit b4a74e0

File tree

6 files changed

+55
-0
lines changed

6 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pathlib import Path
2+
3+
def obtain_words(string):
4+
# Replace non-alphabetic characters with spaces
5+
return "".join(char if char.isalpha() else " " for char in string).split()
6+
7+
def filter_words(words, minimum_length=3):
8+
# Remove words shorter than 3 characters
9+
return [word for word in words if len(word) >= minimum_length]
10+
11+
words = obtain_words(Path("diabetes.txt").read_text().lower())
12+
filtered_words = filter_words(words)
13+
pprint(filtered_words)

0 commit comments

Comments
 (0)