Python Forum
check how many times an item appears in list
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check how many times an item appears in list
#1
Hi, I want to know how many times an item appears in a list.
If I have a list [1, 2, 2, 2] how would I check if 2 occurs 3 times.
Thanks for any help
David
Reply
#2
list_name.count(2)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Hi, thanks for this tried:
if list_name.count(2) = 3
Which gives invalid syntax on the "="
Any ideas?
Thanks
David
Reply
#4
= is assignment, == is comparison. you need the latter

by the way - with closing tags, use forward slash, not back-slash
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks Buran, works now!
You say about Using forward not back slash. I have a more serious problem I'm located in Israel and the text is all right aligned how do I change this to English left aligned
Thanks
David
Reply
#6
As explained by @stranac in your other thread that is browser specific setting
For example for Firefox look at https://support.mozilla.org/bg/questions/1128107
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
My browser - Chrome - is English ltr only in this forum the post text is rtl when i enter it, after posting it's ltr - very confusing.
Reply
#8
Hi, I'm still trying to get value_counts from multiple columns of a csv file this code:
import pandas as pd
df = pd.read_csv("lotto-0807-1218.csv")
print(df['Num 1'].value_counts().to_frame())
Prints the correct value counts for a single column, but how would I get value counts over multiple columns, if I try
print(df['Num 1', 'Num 2'].value_counts().to_frame())
I get:
Error:
KeyError: ('Num 1', 'Num 2')
I want the total for multiple columns not per column

Any help much appreciated.
Thanks
David

Here's the sample lotto-0807-1218.csv

Num 1,Num 2,Num 3,Num 4,Num 5,Num 6
15,21,23,27,36,37
8,11,13,18,25,31
1,3,7,9,15,22
7,8,25,31,32,35
3,5,8,18,19,29
2,12,14,18,22,25
3,5,16,18,21,26
6,16,23,24,30,33
9,20,25,27,34,37
16,18,20,22,24,25
8,9,13,14,27,29
2,7,9,19,24,25
4,8,13,20,27,29
6,7,11,17,22,29
11,17,19,25,27,28
Reply
#9
After Googling around since my last post I finally came up with this solution:
import pandas as pd
df = pd.read_csv("Lottery-numbers-only.csv")
print(pd.value_counts(df.values.flatten()))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 617 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  error handler appears to be turned off. How do I turn it back on? jpotter0 0 565 Nov-26-2022, 11:44 AM
Last Post: jpotter0
Question Finding string in list item jesse68 8 1,801 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  check if element is in a list in a dictionary value ambrozote 4 1,884 May-11-2022, 06:05 PM
Last Post: deanhystad
  Can I check multi condition for 1 item in a easy way? korenron 4 1,537 May-01-2022, 12:43 PM
Last Post: deanhystad
  How to check if a list is in another list finndude 4 1,794 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  how to easily create a list of already existing item CompleteNewb 15 3,387 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,556 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,376 Aug-18-2021, 06:40 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020