split 1234 into ('1', '2', '3', '4')

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ss30
    New Member
    • Aug 2008
    • 6

    split 1234 into ('1', '2', '3', '4')

    Hi,

    I was wondering if there is a simple way to split 1234 into a string of ('1', '2', '3', '4').

    Also, is there a way to test whether an element of a string (or list) is an integer. For instance, if i had a list a = ['1', '2', '3', '4'], the elements are all integers, the function needs to return True.

    However, if the list was ['1', '2', '3', 'x'], how would i distinguish that 'x' is not an integer and therefore the function would return False.

    So far i have used a for loop:
    for i in a:
    if int(i) is type(int):
    return True
    if int(i) is not type(int):
    return False

    but this isn't working at all.

    Thanks for your help :)
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    The str() function takes an integer argument and returns a string. You can also use the list() and tuple() functions to convert strings to lists and tuples. Your loop has two problems. When it finds a number, it returns true before even checking the other strings. You need to make a loop that quits as soon as it finds a non-number string, and only return true if it goes all the way through the list with no problems. Secondly, int(i) will crash your program with a ValueError if it finds a string that does not convert to an integer. Use this to your advantage. Make a try-except block to detect whether the value is an int or not.
    Hope this helps.

    Comment

    • Elias Alhanatis
      New Member
      • Aug 2007
      • 56

      #3
      Also i would suggest that you'd change your test to:
      Code:
      if type(i) is type(1):
         return True
      else:
         return False
      Elias

      Comment

      • Smygis
        New Member
        • Jun 2007
        • 126

        #4
        You can do this.
        Code:
        >>> for i in repr(1234):
        	if i.isdigit():
        		print("OMFG {0} is a digit".format(i))
        
        		
        OMFG 1 is a digit
        OMFG 2 is a digit
        OMFG 3 is a digit
        OMFG 4 is a digit
        >>>
        and if we throw in an 'X':
        Code:
        >>> for i in repr(1234)+"X":
        	if i.isdigit():
        		print("OMFG {0} is a digit".format(i))
        	else:
        		print("OMFG {0} is not a digit".format(i))
        
        		
        OMFG 1 is a digit
        OMFG 2 is a digit
        OMFG 3 is a digit
        OMFG 4 is a digit
        OMFG X is not a digit
        You can also use the isinstance function:
        Code:
        >>> isinstance(10, int)
        True
        >>> isinstance("omg", list)
        False
        >>> isinstance((), tuple)
        True
        >>> isinstance("", (list, str, tuple))
        True

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          Originally posted by Elias Alhanatis
          Also i would suggest that you'd change your test to:
          Yes, but that test doesn't work anyway. "1", "2", and "1234" are still strings, so they aren't of type int.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by ss30
            Hi,

            I was wondering if there is a simple way to split 1234 into a string of ('1', '2', '3', '4').
            Code:
            >>> a=1234
            >>> list(str(a))
            ['1', '2', '3', '4']
            Also, is there a way to test whether an element of a string (or list) is an integer. For instance, if i had a list a = ['1', '2', '3', '4'], the elements are all integers, the function needs to return True.
            Code:
            if not [i for i in a if not  i.isdigit()]: print "True"

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              Originally posted by ghostdog74
              if not [i for i in a if not i.isdigit()]: print "True"
              Wow, that syntax is really wierd. I had no idea you could do that.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Another possibility:
                Code:
                >>> False not in [c.isdigit() for c in '1234']
                True
                >>> False not in [c.isdigit() for c in '1234z']
                False
                >>>

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  If you can do things like that, then what's the map function for?

                  Comment

                  • ss30
                    New Member
                    • Aug 2008
                    • 6

                    #10
                    Originally posted by ghostdog74
                    Code:
                    >>> a=1234
                    >>> list(str(a))
                    ['1', '2', '3', '4']

                    Code:
                    if not [i for i in a if not  i.isdigit()]: print "True"

                    that's brilliant! and so simple!

                    Thanks heaps :)

                    Comment

                    • bvdet
                      Recognized Expert Specialist
                      • Oct 2006
                      • 2851

                      #11
                      Originally posted by boxfish
                      If you can do things like that, then what's the map function for?
                      Built-in function map() has largely been replaced by list comprehensions. Example:
                      Code:
                      map(function, some_list)
                      is equivalent to:
                      Code:
                      [function(x) for x in some_list]

                      Comment

                      Working...