Problem statement:
Count the number of occurrences for each element in the list.
input = a=[1,2,2,3,4,4,4,5]
Output
1 = 1
2 = 2
3 = 1
4 = 3
5 = 1
Solution
a=[1,2,2,3,4,4,4,5]
temp=[]
for item in a:
if item not in temp:
print(f"{item} = {a.count(item)}")
temp.append(item)
"""List Comprehension"""
[(temp.append(item),print(f"{item} = {a.count(item)}")) for item in a if item not in temp]
a=sorted(a)
last_ele=None
for index, item in enumerate(a):
if item != last_ele:
print(f"{item} = {a.count(item)}")
last_ele=item
temp=[]
for item in a:
count=0
for j in a:
if item == j:
count +=1
if item not in temp:
print(f"{item} = {count}")
temp.append(item)
On this page of the site you can watch the video online Python Tutorial: List comprehension in Python with a duration of hours minute second in good quality, which was uploaded by the user GeeksByte 01 January 1970, share the link with friends and acquaintances, this video has already been watched 70 times on youtube and it was liked by 2 viewers. Enjoy your viewing!