0
def word_count(str):
    counts = dict()
    words = str.split()

    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1

    return counts


print(word_count("""the quick brown fox jumps over the lazy lazy lazy dog."""))

Output:

{'the': 2, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 3, 'dog.': 1}

But I want two Output, one like this:

{3 : 'lazy',2 :'the',1 : 'quick',1 : 'brown',1 : 'fox',1 : 'jumps',1 : 'over',1 : 'dog'}

An the other like this organize alphabetically:

 ['brown' : 1,'dog.' : 1,'fox' : 1,'jumps' 1: ,'lazy' 3: , 'over': 1,'quick' 1: ,'the' : 2]

I know that with sorted it can organize alphabetically but I don`t know how to pt it together with the result. I was looking all over Stack-overflow and other places to see if I can find any result but I could not find anything.

martineau
  • 119,623
  • 25
  • 170
  • 301
Leo ley
  • 19
  • 4
  • For me it outputs nothing but an error, because `sorted` return a list – azro May 01 '20 at 16:45
  • 1
    The output `{digit:word}` is not possible, key are unique, you can't have multiple 1 – azro May 01 '20 at 16:47
  • The two outputs you want are invalid. The first has duplicate keys, which is not possible in a dict, and the second one looks like you're confusing dicts and lists. That said, this seems to be a duplicate. I'll post a link in a sec. BTW welcome to SO! Please take the [tour] and read [ask]. – wjandrea May 01 '20 at 16:49
  • Do these answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) and [How can I sort a dictionary by key?](https://stackoverflow.com/q/9001509/4518341) – wjandrea May 01 '20 at 16:49
  • 1
    thank you @wjandrea for helping me out, this two lines of close are the closes results that I want :), really thank-you : print({k: v for k, v in sorted(x.items(), key=lambda item: item[1])}) print(dict(sorted(x.items()))) – Leo ley May 01 '20 at 16:59

2 Answers2

0

You could do something like this with the result of word_count

res = word_count("""the quick brown fox jumps over the lazy lazy lazy dog.""")
word_list = list(res.items())
inverted_list = [t[::-1] for t in word_list]

by_word = sorted(word_list, key=lambda t: t[0], reverse = True) 
by_count = sorted(inverted_list, key=lambda t: t[0], reverse = True) 

print(by_word)
print(by_count)

Keep in mind that the outputs you expected are not possible. Instead, this gives you arrays of tuples with the values sorted as you wanted them.

Alejandro De Cicco
  • 1,216
  • 3
  • 17
0

As others have said, the results you want don't make sense as they aren't valid. However if you want a sorted set that matches the final example you provide you can use something like the following.

data = """the quick brown fox jumps over the lazy lazy lazy dog."""

def word_count(str):
    result = {}
    words = str.split()
    for word in words:
      if word not in result:
        result[word] = words.count(word)
    return dict(sorted(result.items()))

print(word_count(data))

Which produces...

{'brown': 1, 'dog.': 1, 'fox': 1, 'jumps': 1, 'lazy': 3, 'over': 1, 'quick': 1, 'the': 2}
Fraser
  • 15,275
  • 8
  • 53
  • 104
  • Note this is only guaranteed to work on Python 3.7+ since dicts were unordered before. For earlier versions, you could return a list of tuples, i.e. `sorted(result.items())` – wjandrea May 01 '20 at 17:01
  • 1
    True, but no version was specified so I just went with simplest answer that was closest to the OPs code. – Fraser May 01 '20 at 17:02
  • Thank-You for your help :) – Leo ley May 01 '20 at 17:03
  • 1
    @Leoley You can pay back in a way by [upvoting good answers and accepting the best one](/help/someone-answers) :) – wjandrea May 01 '20 at 17:19