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)
На этой странице сайта вы можете посмотреть видео онлайн Python Tutorial: List comprehension in Python длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь GeeksByte 01 Январь 1970, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 70 раз и оно понравилось 2 зрителям. Приятного просмотра!