0
我在做練習3從http://cscircles.cemc.uwaterloo.ca/15b-python-pushups/,我做了代碼的工作,但想知道是否有可能在更少的行中做到這一點?這裏是我的解決方案:Python列表的理解失敗,語法錯誤
los = [] # short for list of strings
while True:
s = input()
if s == '###': break
los += s.lower().split() # making a list of lower case words from the input sentences
test = []
for x in los:
test += str(los.count(x)) # made a new list of the frequency of each word
a = test.index(max(test)) # variable a provides the location of them most frequent word
print (los[a]) # we know the position of the most frequent string, so find it in los.
# a is not needed but it looks neater
所以這部分尤其是我不是很滿意:
for x in los:
test += str(los.count(x))
我要重新寫,如:
test += str(list.count(x)) for x in los
但告訴我無效的語法..所有提示?
使用http://codereview.stackexchange.com/這種類型的問題\ – 2014-08-28 08:31:37
只是把這個後while循環:'打印(MAX(LOS, key = los.count))' – grc 2014-08-28 08:38:57
@grc謝謝你的工作,但我不明白這條線是如何工作的(完全),你能解釋一下嗎? – MathsIsHard 2014-08-28 08:55:33