[Tutor] word printing issue

Steven D'Aprano steve at pearwood.info
Thu Jun 20 22:10:30 EDT 2019


Hi Sean,

Your subject line says "word printing issue", but the body of your email 
says nothing about an issue printing words.

Have you tried print(word)?

Further comments and questions below.


On Thu, Jun 20, 2019 at 08:44:06PM +1000, mhysnm1964 at gmail.com wrote:
> I have a list of strings that I want to break them into separate words, and
> a combination of words then store them into a list. Example below of a
> string:
> 
> "Hello Python team".
> 
> The data structure:
> 
> [ ['Hello'],
>   ['Hello', 'Python'],
>   ['Hello', 'Python', 'team'],
>   ['Python'],
>   ['Python', 'team'],
>   ['team'] ]


I've taken the liberty of correcting some obviouis typos in the data 
structure.


Sean wrote:

> I want to know if there is a better method in doing this without the
> requirement of a module.

Better than what? How are you doing it now?

Why don't you want to use a module?


Sean wrote:

> Eventually I want to count the number of hits in a
> list of strings based upon the word combinations regardless where they are
> located in the string. This last part is something I am struggling with to
> get results that are correct.

I'm struggling to understand what you mean. I don't understand what the 
word combinations part has to do with the problem.

If all you want to do is count each word, you could try this:

from collections import Counter
text = "Hello Python team"
c = Counter(text.split())
print(c)


which will print something like:

Counter({'team': 1, 'Python': 1, 'Hello': 1})

(the order of the counts may be different).


> I have stored them in a dictionary and get
> unexpected totals. 

Either your expectations are wrong, and the totals are correct, or your 
expectations are correct, and the code counting them is wrong.

Without knowing either your expectations or the code counting the 
totals, I cannot guess which is the case.


> Was thinking of using collection and still working on it.
> If the above could be improved. I would be grateful.

Without knowing what you are doing, it is hard to suggest improvements.

"Dear cooking experts, I'm baking a cake and it turned out all wrong. 
What can I do to make it better? Thanks in advance."



-- 
Steven


More information about the Tutor mailing list