Frequently Asked Python Program 15: Count Occurrences of an element in a list

Поделиться
HTML-код
  • Опубликовано: 25 янв 2025

Комментарии • 30

  • @ramanaboinaharish9391
    @ramanaboinaharish9391 2 месяца назад +1

    n=[15,6,7,10,12,20,10,28,10]
    a=10
    count=0
    for i in n:
    if i==a:
    count=count+1
    print(count)

  • @sumanpalisetty6096
    @sumanpalisetty6096 4 года назад +23

    I don't want to assign a value of x to be counted, i want a program which automatically counts all the occurances

  • @LilMizzCheeky1
    @LilMizzCheeky1 3 года назад +3

    I have been searching for this exact video for hours this has helped me so much!!!

  • @kvelez
    @kvelez Год назад

    def count(arr, i, dictionary):
    if i >= len(arr):
    return dictionary
    if arr[i] in dictionary:
    dictionary[arr[i]] += 1
    else:
    dictionary[arr[i]] = 1
    return count(arr, i+1, dictionary)
    print(count([1,1,2,3,4,5,1],0,{}))

  • @buppergudemsowmya3832
    @buppergudemsowmya3832 3 года назад +1

    Which method tells the no. of objects stored in object of particular type

  • @VIPINKUMAR-hc9sz
    @VIPINKUMAR-hc9sz 2 года назад +1

    How we will print the index value of the given number.(If we would like to print the last index occurrence of the given number in the list, what modification we should do in the above described code?) Please reply.

  • @adibabelayete1926
    @adibabelayete1926 3 года назад +2

    how to count the number of occurrences of the second parameter within the first parameter string? like - number of occurrences "iss" string in "mississippi".thanks

    • @sumanth591
      @sumanth591 Год назад

      def countOccurancesInString(s, n):
      dic = Counter(s)

      print("The occurances of each and every letter are ",dic)

      print("The occurances of {} are {}".format(n,dic[n]))

      countOccurancesInString("mississippi", "s")
      The occurances of each and every letter are Counter({'i': 4, 's': 4, 'p': 2, 'm': 1})
      The occurances of s are 4

  • @creativeStudio-l1o
    @creativeStudio-l1o 3 года назад +1

    what if we are supposed to count duplicates in a list entered by user 'n' times

  • @MatrixMoney-uo6pk
    @MatrixMoney-uo6pk 4 месяца назад

    How can I choose to count how many times 2 characters appear in a text ?
    For example how many "th" in text "abc" ?

  • @planet_of_funtech
    @planet_of_funtech 3 года назад +5

    What if count of two numbers are equal? How to solve that one?

  • @AAli560045
    @AAli560045 4 года назад +6

    How to do without variable x??

  • @SurajPatil-wh9jt
    @SurajPatil-wh9jt 2 года назад +1

    Thank you Sir.By using loop method ,it is giving output multiple times like "10 has occurred 3 times".This statement repeats multiple times.Any Reason ?

    • @vlow2k
      @vlow2k 11 месяцев назад

      keep the print statement outside of the loop body

  • @equilibrium3449
    @equilibrium3449 2 года назад

    how do i ascend or descend it from collection library?

  • @VIPINKUMAR-hc9sz
    @VIPINKUMAR-hc9sz 2 года назад

    my_list = [10,15,25,10,35,45,10,5,2,4,1]
    x = 10
    length = len(my_list)
    count =0
    for i in range(length):
    if my_list[i] ==x:
    a = i
    print(a)
    Please check, is this correct to find the last occurrence

  • @Braven77
    @Braven77 Год назад +1

    Thank you! But can you explain please how to count ALL elements so it will look like:
    10 - 2
    5 - 1
    6 - 1
    etc..

  • @sandeeprn6959
    @sandeeprn6959 2 года назад

    write a python programme to check the given list is having first value as integer or not using if else statement. Anyone help me with that 😁

  • @As-zc3kt
    @As-zc3kt 2 года назад +1

    nice video

  • @Bmenthuperson
    @Bmenthuperson 3 года назад

    Sir I only want the frequency not the character 😭😭

  • @kvelez
    @kvelez Год назад

    def count(arr, i, dictionary):
    if i >= len(arr):
    return dictionary
    if arr[i] in dictionary:
    dictionary[arr[i]] += 1
    else:
    dictionary[arr[i]] = 1
    return count(arr, i+1, dictionary)
    print(count(["hello", "world"],0,{}))