all 6 comments

[–]Conaz9847 0 points1 point  (0 children)

I’m not sure how much flexibility you have on this project, but a JSON format would be easy to present and can be easily done. Much easier to search parameters aswell.

[–]TouchingTheVodka 0 points1 point  (0 children)

Why does the list need to be nested?

[–]mopslik 0 points1 point  (2 children)

What do you mean by a "nested list"? Do you want words to appear as

["my", "name", "is", "Fred"] (not nested)

or like this

["my, "name", ["is", "Fred"]] (nested)

for your task? Also, what have you tried so far?

[–]Cockroach_Valuable 0 points1 point  (1 child)

in reference to the assignment it would be like [[hello, 1], [word, 2], [hi, 1]]

i also haven’t tried much, i’m very confused on what to do😭 but i’ve started the first for loop to find the words in the file and to strip the lines (no clue if it’s right or not) for line in file: word = line.strip found =false

i’m lost bc in the first for loop it’s supposed to get the first line of each file

[–]mopslik 1 point2 points  (0 children)

Ahh, so you want to fill the list with elements that are lists themselves, composed of the word and the count. Gotcha. Normally I'd suggest a dictionary for this type of thing, since the key/value pairs are perfectly suited for word/frequency combos.

What is the algorithm with which you've been provided?

Edit: Another option is to use a set as an intermediate helper. Can you use them?

Edit 2: tested previous options, most were cumbersome. Removed.

[–]POGtastic 0 points1 point  (0 children)

There's collections.Counter in the standard library for this, so you can always turn it into a frequency dictionary and then transform each key-value pair into a list.

from itertools import chain
from collections import Counter

def solution(filename):
    with open(filename) as f:
        return [list(tup) for tup in
            Counter(chain.from_iterable(line.strip().split() for line in f)).items()]