python - Finding word overlap in the list -
i need write function given list of words returns true
if there repeating words there, or false
otherwise.
this first programming attempt , having problem following:
def over(list1): in list1: j in list1: if == j: return false return true
i expecting, if run this:
over(["alex", "alex"])
it must return true
since overlap.
if run:
over(["alex", "david"])
then must return false
since not overlap.
please give me advice on how solve this.
if need count occurrences of list elements using counter
collection
module
solution(in python 3.2):
from collections import counter def occurdict(items): dict_ans = dict(counter(items).most_common()) return dict_ans list=['car', 'bus', 'truck', 'train', 'truck', 'train', 'bus', 'bus', 'train'] print(occurdict(list))
output:{'truck': 2, 'car': 1, 'train': 3, 'bus': 3}
and if want check if items occures multiple times then, solution:
from collections import counter def over(items): l = counter(items).most_common() if l[0][1] > 1: return true else: return false list=['car', 'bus', 'truck', 'train', 'truck', 'train', 'bus', 'bus', 'train'] print(over(list))
for input: list = ['car', 'bus', 'truck', 'train']
output: false
Comments
Post a Comment