1

If I have a list such as :

List = [12,6,3,5,1.2,5.5] 

Is there a way I can check if all the numbers are whole numbers? I tried something like

def isWhole(d): 
if (d%1 == 0 ) : for z in List return true.

That is obviously terribly wrong. What can I do?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
bob9123
  • 725
  • 1
  • 9
  • 31
  • Maybe related: [How to compare type of an object in Python?](http://stackoverflow.com/q/707674/2596334). – Scott Solmer Nov 12 '14 at 16:54
  • What is a whole number (programatically speaking)? Are only `integers` whole numbers? Then what about `bool` (`True` and `False`) which inherits from `int`? Or is a whole number anything that `1` divides into evenly? That would make `complex(1,0)` and `fractions.Fraction(1,1)` whole numbers. Is that what you want? – Steven Rumbalski Nov 12 '14 at 17:29
  • Voting to close as "Unclear what you are asking" because the definition of "whole number" is unspecified. – Steven Rumbalski Nov 12 '14 at 17:37
  • Also, in mathematics whole numbers are not negative. So should negatives be excluded? – Steven Rumbalski Nov 12 '14 at 17:38

4 Answers4

6

So you want integers and floats that are equal to integers?

def is_whole(d):
    """Whether or not d is a whole number."""
    return isinstance(d, int) or (isinstance(d, float) and d.is_integer())

In use:

>>> for test in (1, 1.0, 1.1, "1"):
    print(repr(test), is_whole(test))


1 True # integer 
1.0 True # float equal to integer
1.1 False # float not equal to integer
'1' False # neither integer nor float

You can then apply this to your list with all and map:

if all(map(is_whole, List)):

or a generator expression:

if all(is_whole(d) for d in List):
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Okay thanks for this. I think this is the right direction i need to go. So using the "if all(map(is_whole, List)):" How do it print out if the individual elements come out as True or False? – bob9123 Nov 12 '14 at 17:08
  • Swap `if any(...):` for `list(...)` or a list comprehension, e.g. `[is_whole(d) for d in [12, 6, 3, 5, 1.2, 5.5]] == [True, True, True, True, False, False]`. – jonrsharpe Nov 12 '14 at 17:15
  • 1
    `is_whole` evaluates `True` and `False` as whole numbers. Should it? – Steven Rumbalski Nov 12 '14 at 17:25
  • @StevenRumbalski that's a good question - as `bool` is a subclass of `int`, `isinstance(d, int)` evaluates `True` for `d = True`. If this were *not* the desired behaviour, an initial check `if not isinstance(d, bool)` would need to be added. – jonrsharpe Nov 12 '14 at 17:27
  • Okay. Got it to work. Thanks very much for this. Is there an output for this where if one evaluates to be True then I can print something specifically? – bob9123 Nov 12 '14 at 17:33
  • ...what? Do you mean if *exactly one* evaluates to `True`? `sum(...)` will give you the number that evaluate to `True`, or have a look at [`any`](https://docs.python.org/3/library/functions.html#any). – jonrsharpe Nov 12 '14 at 17:34
  • Yeah so say in my example. If one of my elements evaluates to be True then I want it to be able to print out. – bob9123 Nov 12 '14 at 17:37
  • `for item in List:if is_whole(item):print item` or `print([item for item in List if is_whole(item)]) # may print an empty list` – Steven Rumbalski Nov 12 '14 at 17:40
  • Sorry I wasn't really clear. Say if one of items in the list = true. I want to print out a separate message. For example something like if item for item in list if is_whole(item): print("Message"). – bob9123 Nov 12 '14 at 18:07
  • 1
    That hasn't clarified anything (for one thing, whitespace is important in Python - it's unreadable in comments, especially when not even formatted `as code`). Why don't you make an actual effort - try some things in the interpreter (e.g. `sum`, as I've already mentioned)? Without seeing a minimal example of the code along with inputs and expected and actual outputs, how are we supposed to know what's wrong with whatever you have? – jonrsharpe Nov 12 '14 at 18:09
  • I've been trying for for the past 20 minutes. I'm just very new to Python. Basically, so far if I have a list of elements and they all print out true and false. For example 4.0= True, 2.85= false, 2.22= false. I want to try and see that if at least one of the elements came out as true, which will be the 4.0 in the example then I will print out a separate string saying True. – bob9123 Nov 12 '14 at 18:16
  • Then, as I have *already suggested*, you want `any` instead of `all`. – jonrsharpe Nov 12 '14 at 18:17
  • Thanks for the help. Appreciate it. – bob9123 Nov 12 '14 at 18:24
1

Simple solution for a list L:

def isWhole(L):
    for i in L:
        if i%1 != 0:
            return False
    return True
Ogaday
  • 469
  • 3
  • 13
1

List = [12,6,3,5,1.2,5.5]

for i in List:

if i%1 != 0 :
    print(False)
    break
0

if your list is lst = [12,6,3,5,1.2,5.5] you can do this.

lst = [12,6,3,5,1.2,5.5]
print(all(isinstance(i,int) for i in lst))

it prints "True" if your list contains whole numbers, if not "False"