0

lines = Counter({'you': 29, 'i': 24, 'my': 17, 'more': 13, 'one': 12, 'night': 12, 'go': 11, 'yeah': 10})

my code is this:

word = Counter(lines)


from collections import defaultdict

reversed_word = defaultdict(list)

for value, count in word.most_common(top_words):
    reversed_word[count].append(value)
for key in sorted(reversed_word, reverse= True):
    print("The following words appeared {} times each: {}".format(key,', '.join(sorted(reversed_word[key]))))

if top_words = 5

output:

The following words appeared 29 times each: you

The following words appeared 24 times each: i

The following words appeared 17 times each: my

The following words appeared 13 times each: more

The following words appeared 12 times each: night, one

but with my code i get:

The following words appeared 29 times each: you

The following words appeared 24 times each: i

The following words appeared 17 times each: my

The following words appeared 13 times each: more

The following words appeared 12 times each: night

i get the output I need if I change top_words to 6:

The following words appeared 29 times each: you

The following words appeared 24 times each: i

The following words appeared 17 times each: my

The following words appeared 13 times each: more

The following words appeared 12 times each: night, one

how do I fix this?

ggchc
  • 1
  • 5
  • Please tag your questions with python so they can be seen by a larger audience. – cs95 May 28 '18 at 04:41
  • can you please not make it as a duplicate. It is not it is a different question – ggchc May 28 '18 at 04:44
  • you're trying to invert a mapping. I don't understand how they're different – cs95 May 28 '18 at 04:45
  • I'm trying fix my code. I don't have a problem with mapping. The problem is with the top_words – ggchc May 28 '18 at 04:46
  • Do this: `top5 = sorted(set(word.values()), reversed=True)[:5]`, and then get all items with these unique `top5` values. – cs95 May 28 '18 at 04:48
  • that doesn't work – ggchc May 28 '18 at 04:51
  • Too bad, no [mcve] = no help – cs95 May 28 '18 at 04:51
  • You didn't do it right, that's why it doesn't work. The point is to get the keys corresponding to the top 5 unique values. If you've come this far, then with the hint I'd given you, you should've got it at once. – cs95 May 28 '18 at 04:53
  • top_words = sorted(set(word.values()), reversed =True)[:top_words] – ggchc May 28 '18 at 04:54
  • Who asked you to reassign it to `top_words`? – cs95 May 28 '18 at 04:55
  • the top_words is an input. It changes to whatever the user asks for – ggchc May 28 '18 at 04:56
  • I understand, but why did you reassign it? You're supposed to use the `top5` list to filter out those entries in `word` which have counts present in `top5` – cs95 May 28 '18 at 04:56
  • but it isn't always 5 correct? – ggchc May 28 '18 at 04:58
  • You clearly don't understand your assignment. Two different words can have the same counts, so the != 5. – cs95 May 28 '18 at 04:59
  • how do I change that the top_words doesn't count the words itself but the count values. If that makes sense. – ggchc May 28 '18 at 05:01
  • I'm trying to explain how without feeding the answer to you in a silver spoon. This is your assignment, and you've come this far, so please try to figure it out yourself. By the way, please don't keep creating new accounts for the purpose of getting your question answered. – cs95 May 28 '18 at 05:02
  • I've been trying to get help but you keep marking my question as duplicate without even reading the question so no one answers. – ggchc May 28 '18 at 05:03
  • can you please release the duplicate because it is not – ggchc May 28 '18 at 05:12
  • I _tried_ helping you. Any more and I'd literally be spoon-feeding you the answer. So go back to your terminal and figure it out... hint: you're a couple of lines of code away. Take my `top5` hint and it'll solve your problem. Or don't, and keep blaming me for the fact that you don't want to try. – cs95 May 28 '18 at 05:24
  • I'm sorry I'm new to this so understanding this is hard. Thanks for the hint I'll try to figure out what it means. – ggchc May 28 '18 at 06:05

0 Answers0