Python Forum
Error in the method "count" for tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in the method "count" for tuple
#1
I just wrote this simple code to count the number of 1 in this tuple. Ironically, it returns 2 instead of 1. I have the last version of Python. Also, I checked the program with Colab, and I got the same wrong result. It is interesting that this problem occurs only for "1"!!

mixed_tuple = (1, 3.4, "ali", True, [0, 3, 3]) print(mixed_tuple.count(1))
The result will be 2.
Reply
#2
Oh, yes and the result is right.

In [1]: 1 == True Out[1]: True In [2]: int.mro() Out[2]: [int, object] In [3]: bool.mro() Out[3]: [bool, int, object] In [4]: ((1,)).count(1) Out[4]: 1 In [5]: ((1, True)).count(1) Out[5]: 2 In [6]: ((0, True)).count(1) Out[6]: 1 In [7]: ((0, False)).count(1) Out[7]: 0 In [8]: ((0, False)).count(0) Out[8]: 2
A boolean is a subtype of int.
It's getting worse, if you use for keys boolean and int as keys:
In [1]: d = {0: "Foo"} In [2]: print(d) {0: 'Foo'} In [3]: d[False] = "This is False" In [4]: # now what do you expect? In [5]: print(d) {0: 'This is False'}
Do not mix int with bool in sequences and mappings.
Sometime the property could be easily used.

If you have a list/tuple with only boolean insde, you can sum up True, which is a subtype of int and is equal to 1.
In [1]: import random In [2]: data = random.choices([True, False], k=10) In [3]: print(data) [True, False, False, False, True, False, True, True, False, True] In [4]: # count number of True In [5]: print(sum(data)) 5
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thank you for your detailed response.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange argument count error rowan_bradley 3 2,308 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Row Count and coloumn count Yegor123 4 3,787 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  List of error codes to find (and count) in all files in a directory tester_V 8 6,538 Dec-11-2020, 07:07 PM
Last Post: tester_V
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 5,631 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 8,175 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  Error: Nested method ? JohnnyCoffee 5 4,575 May-03-2020, 02:43 PM
Last Post: JohnnyCoffee
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 5,229 May-21-2019, 11:39 AM
Last Post: avorane
  Error while fetching data from PostgreSQL tuple indices must be integers or slices, n Sandy777 6 8,774 May-12-2019, 11:41 AM
Last Post: Sandy777
  Type error when reading in different data types on an __init__ method Dylanmull 3 4,489 May-09-2019, 02:05 PM
Last Post: buran
  Getting error when called through instance method aankrose 2 3,910 Mar-02-2019, 07:19 PM
Last Post: aankrose

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.