0

These are the strings I'm slicing.

In the solution to this exercise, the programmer used positive and negative indices for slicing. I'd just like clarification about how the -26 index works in the solution (shared below).

Thank you!

counter_dict = {}
with open('Training_01.txt') as f:
    line = f.readline()
    while line:
        line = line[3:-26]
        if line in counter_dict:
            counter_dict[line] += 1
        else:
            counter_dict[line] = 1
        line = f.readline()

print(counter_dict)

After seeing the solution, I modified my answer, but instead of complete categories, my solution gave me incomplete categories.

with open('Training_01.txt', 'r') as open_file:
    text = open_file.read()

new_page = text.split('\n')
categories = []
for category in new_page:
    category = category[3:-26]
    categories.append(category)
categories = set(categories)
print(len(categories))
for category in categories:
    print(category)
Dabs
  • 1

0 Answers0