[問題] 關於重複搜尋

作者: paulluopaull (ppoo)   2017-08-23 07:15:29
小弟最近在做PYTHON習題
Problem
Given: A string ss of length at most 10000 letters.
Return: The number of occurrences of each word in ss, where words are
separated by spaces. Words are case-sensitive, and the lines in the output
can be in any order.
Sample Dataset:
We tried list and we tried dicts also we tried Zen
Sample Output:
and 1
We 1
tried 3
dicts 1
list 1
we 2
also 1
Zen 1
但是小弟做同樣練習時會重複出現有重複的字:
we 3
作者: wennie0433   2017-08-23 11:19:00
word那個回圈每次都會重製你的pare所以根本沒紀錄到迴圈外面宣告一個dup = dict()然後dup.update(pare)最後那個迴圈就直接搬出來不要在迴圈裡面pare換成dup然後他題目大小寫應該市分開算lower()要拿掉
作者: zerof (貓橘毛發呆雕像)   2017-08-24 10:35:00
{k: lines.count(k) for k in set(lines.split())}

Links booklink

Contact Us: admin [ a t ] ucptt.com