0

I am a beginner to code this year, and I really hope I can move on and understand how to tackle some truly difficult tasks in the future. I am, however, pretty stumped by this one. I am writing a code that will count the instances of a word in an input and to just simply throw them into a list, no sorting of any kind. This is gonna be some very primal code, but I would think it would do it's simple job correctly, however, it seems to want to throw an 'a' in the middle of the input all the way at the beginning for no apparent reason. Why is this happening?

TLDR: Why is my code sorting an 'a' to the beginning of my dictionary?

Code:

wordDict = {}
text = input("Enter some text: ")
text = text.lower()
text = text.split()
print text
for word in text:
    if word in wordDict:
        wordDict[word] += 1
    else:
        wordDict[word] = 1
print wordDict

Thanks for your help and time :)

Edit: Picture Added

The Problem with the Code, According to Python

  • 1
    I think this question might help you: https://stackoverflow.com/questions/10371085/python-how-to-disable-auto-sort-when-creating-dictionary – Silveris Oct 25 '19 at 14:59
  • prior to python 3.7, python dictionaries are not sorted. There are various other data structures that preserve order if you need it (e.g. `list`, `tuple`, `OrderedDict`) – Dan Oct 25 '19 at 14:59
  • This one also contains explanations: https://stackoverflow.com/questions/1867861/how-to-keep-keys-values-in-same-order-as-declared – Silveris Oct 25 '19 at 15:00
  • What's your input, the output, and what did you expect? See also [mre]. Also dictionaries are unordered in Python 2, so there's no garantuee with regard to ordering (for all practical purposes treat the order as random). If you want insertion ordered dicts you have to use python 3.6 or later. – MSeifert Oct 25 '19 at 15:01
  • @Silveris thank you for the quick response time! I searched for a similar problem but couldn't find it, thanks again :) – robert lewison Oct 25 '19 at 15:02
  • 1
    Is there a reason to use Python 2? If not I'd suggest to switching to Python 3. – gstukelj Oct 25 '19 at 15:02
  • @Dan thank you. so is the way dictionaries sorted prior to 3.7 just completely random and completely out of the user's hand? seems counter-intuitive when everything else in python is so easily manipulable and adjustable by the user. – robert lewison Oct 25 '19 at 15:04
  • @MSeifert the input is simply any user input, such as 'a cat jumped over a hill', in which my expected output of printing wordDict would be {'a': 2, 'over': 1, 'cat': 1, 'jumped': 1, 'hill': 1}. however, when running it with, say, 'if you ever ever ever meet a whale you must never never never touch his tail', the output of printing wordDict is {'a': 1, 'if': 1, 'you': 2, 'ever': 3, 'meet': 1, 'whale': 1, 'must': 1, 'never': 3, 'touch': 1, 'his': 1, 'tail': 1}. I just wanted to know why it is taking the 'a' that is in the middle of the input, and putting it at the very beginning of the output. – robert lewison Oct 25 '19 at 15:07
  • 1
    @robs, it's certainly not random. my understanding is that they are designed to maximise lookup speed, and slower alternatives such as `OrderedDict` are available if the ordering is important – Dan Oct 25 '19 at 15:08
  • @gst I am only a few weeks into Python (as I'm sure you can tell by the terrible code), and as a course I am taking for my high school, I have to complete the course on codehs.com, which for this specific course runs in 2.7. I have no idea why, but for this specific problem it just has to be in 2.7 – robert lewison Oct 25 '19 at 15:10
  • @Dan in this code, where would I use `OrderedDict` to make it ordered, if that makes sense. – robert lewison Oct 25 '19 at 15:11
  • @robs `from collections import OrderedDict`, `wordDict = OrderedDict()` should do the trick – Dan Oct 25 '19 at 15:13
  • @Dan it's probable that I'm simply not putting it in the right place, however when I use it at line 10 and move the final line down 1, I get `ParseError: bad input on line 10` – robert lewison Oct 25 '19 at 15:28
  • @robs: The reason we're suggesting a switch to Python 3 is: 1) In Python 3.7+ (CPython/PyPy 3.6), normal `dict`s are insertion ordered, which fixes your problem for free, and 2) Python 2 stops being supported *completely* at the end of this year. Even critical security problems will not be addressed. If you're just beginning to learn Python, it should *not* be Python 2. – ShadowRanger Oct 25 '19 at 17:28

1 Answers1

0

following on from comment, try this:

from collections import OrderedDict

wordDict = OrderedDict()
text = input("Enter some text: ")
text = text.lower()
text = text.split()
print text
for word in text:
    if word in wordDict:
        wordDict[word] += 1
    else:
        wordDict[word] = 1
print wordDict
Dan
  • 1,575
  • 1
  • 11
  • 17