Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Need help evaluating an expression

Hi all, I am very new to python and for my class we have to be able to get the answer to this expression without using python, so the answer is there but we would not be able to see that during the test. I was wondering if someone could explain to me how you get the answer of {'o': 3, 'k': 2, 'e': 7, 'm': 2, 'n': 1, 'a': 2, 'd': 1} from the expression
{ c:S.count© for c in "".join([ w[1:] for w in S.split() if "e" not in w[-3:-1] and "o" in w[:4] ]) } with S="coffee coke tea lemonade milk sprite". Ive been looking at it for the past hour and cannot figure out why the "i" and "L" are not counted but the "m" and the "k" are counted in milk and I dont really understand why "o" and "e" are always counted even if the word does not meet the if conditions. Thanks


S = "coffee coke tea lemonade milk sprite"
>>> { c:S.count© for c in "".join([ w[1:] for w in S.split() if "e" not in w[-3:-1] and "o" in w[:4] ]) }
{'o': 3, 'k': 2, 'e': 7, 'm': 2, 'n': 1, 'a': 2, 'd': 1}
(Sep 22, 2019 08:13 AM)Wic12 Wrote:  Hi all, I am very new to python and for my class we have to be able to get the answer to this expression without using python, so the answer is there but we would not be able to see that during the test. I was wondering if someone could explain to me how you get the answer of {'o': 3, 'k': 2, 'e': 7, 'm': 2, 'n': 1, 'a': 2, 'd': 1} from the expression
{ c:S.count© for c in "".join([ w[1:] for w in S.split() if "e" not in w[-3:-1] and "o" in w[:4] ]) } with S="coffee coke tea lemonade milk sprite". Ive been looking at it for the past hour and cannot figure out why the "i" and "L" are not counted but the "m" and the "k" are counted in milk and I dont really understand why "o" and "e" are always counted even if the word does not meet the if conditions. Thanks


S = "coffee coke tea lemonade milk sprite"
>>> { c:S.count© for c in "".join([ w[1:] for w in S.split() if "e" not in w[-3:-1] and "o" in w[:4] ]) }
{'o': 3, 'k': 2, 'e': 7, 'm': 2, 'n': 1, 'a': 2, 'd': 1}

This is an awful question to give a newcomer to Python... Think about the order of operations, just like in mathematics. What is happening first and work your way out. If you truly can't figure it out let me know. Write it out long winded.

I was once a college student too. IT not CS. But questions like this have no reasoning on purpose. They're intended to confuse you and they work quite well. I had a Linux course full of stupid commands just like this where is you don't know a dash or a slash there. You won't be able to answer. Pretty harsh.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Poojar102Nov 1, 2021 10:32 AM 4 187 Nov 1, 2021 10:32 AM
Last Post: Maquina
[C#] by GraeskMytologiApr 6, 2019 09:55 AM 1 156 Apr 6, 2019 09:55 AM
Last Post: 0xDEAD10CC
[.NET] by WritzSep 23, 2017 02:06 PM 12 329 Sep 23, 2017 02:06 PM
Last Post: 0xDEAD10CC