need help converting a list into a string or vice versa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • victory2006
    New Member
    • Nov 2008
    • 10

    need help converting a list into a string or vice versa

    I need help converting a list into a string
    ex/

    i want to compare this, "110"(strin g) to this [110](list)

    to see if they are identical. So, how would you do this? convert the string into a list? the list into a string? then compare the two?

    thanks in advance
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Is the 110 in the list an integer?
    [code=Python]>>> alist = [110]
    >>> astring = '110'
    >>> alist[0] == astring
    False
    >>> str(alist[0]) == astring
    True
    >>> alist == [int(astring)]
    True
    >>> [/code]

    Comment

    • victory2006
      New Member
      • Nov 2008
      • 10

      #3
      here how about this, [[1,1,0],[0,1,0],[0,0,1]] and compare it with the string "110"

      in that list of lists, is it possible to compare that string ("110") with the any of the lists inside the list of lists?

      what im trying to say here is, is it possible to match the string with any one of them in there

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Yes.
        [code=Python]data = [[1,1,0],[0,1,0],[0,0,1]]
        s = "110"

        slist = [int(i) for i in list(s)]

        if slist in data:
        print 'Variable is at list index %s.' % data.index(slis t)
        else:
        print 'Variable is not in the list.'[/code]

        Comment

        • victory2006
          New Member
          • Nov 2008
          • 10

          #5
          what if your variable "s" was 10, such that s is always one digit less than from the lists inside the list of lists.

          because i entered s as 10 and my "data" was at [[0, 1, 0], [0, 1, 0], [1, 1, 0]]

          but it said 'Variable is not in the list.' but "10" is found from within all of those lists (the last two numbers in each list)

          again, thanks alot for the help

          oh yeah, and also in your code, "'Variable is at list index %s.' % data.index(slis t) " those % mean module?

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            You could do something like this:
            [code=Python]s = '10'
            data = [[1,1,0],[0,1,0],[0,0,1]]
            dataList = [''.join([str(i) for i in item]) for item in data]
            for i, item in enumerate(dataL ist):
            if s in item:
            print 'Variable is at data[%s][%s].' % (i, item.index(s))[/code]

            The modulo operator (s % d) produces a formatted string given a format string s and a tuple or dictionary d.
            [code=Python]>>> "The %(car)s was clocked at %(speed)d MPH." % {'car': "Lamborghin i", 'speed': 220}
            'The Lamborghini was clocked at 220 MPH'
            >>> "The %s was clocked at %d MPH." % ("Lamborghin i", 220)
            'The Lamborghini was clocked at 220 MPH.'
            >>> [/code]

            Comment

            • victory2006
              New Member
              • Nov 2008
              • 10

              #7
              Oh Thanks! this is what i got

              Code:
              Pattern to be searched: 10
              OK
               
              The board is 
               
               
              Col1   Col2   Col3   
              Row1   1   0   0   
              Row2   1   1   0   
              Row3   1   1   0    
               
              [[1, 0, 0], [1, 1, 0], [1, 1, 0]]
              Variable is at data[0][0].
              Variable is at data[1][1].
              Variable is at data[2][1].
              BUT, how do u.....

              that it checks for pattern "10" in data[0][0]. then data[0][1], then data[0][2]..and so on all the way so that it checks EVERY single couple and tells us whether in each case that it is a identical or not

              ex/

              "10"
              data = [[1, 0, 0], [1, 1, 0], [1, 1, 0]]
              Variable is at data[0][0].
              variable is not in data [0][1]
              ...and so on

              Thanks

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Either the variable is in the sublist (row) or not.
                Code:
                s = '10'
                dataList = [''.join([str(i) for i in item]) for item in data]
                for i, item in enumerate(dataList):
                    if s in item:
                        print 'Variable is at data[%s][%s].' % (i, item.index(s))
                    else:
                        print 'Variable is not in row %s.' % i
                Output:
                Code:
                >>> Variable is at data[0][1].
                Variable is at data[1][1].
                Variable is not in row 2.
                >>>

                Comment

                • victory2006
                  New Member
                  • Nov 2008
                  • 10

                  #9
                  so u cant show every specific spot if it is similiar or not?

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Yes, I suppose so. I don't understand what this will do for you though.
                    Code:
                    data = [[1,1,0],[0,1,0],[0,0,1]]
                    s = '10'
                    dataList = [''.join([str(i) for i in item]) for item in data]
                    for i, item in enumerate(dataList):
                        for j in range(len(item)-len(s)+1):
                            if s == item[j:j+len(s)]:
                                print 'Variable is at data[%s][%s].' % (i, j)
                            else:
                                print 'Variable is NOT at data[%s][%s].' % (i, j)
                    Output:
                    Code:
                    >>> Variable is NOT at data[0][0].
                    Variable is at data[0][1].
                    Variable is NOT at data[1][0].
                    Variable is at data[1][1].
                    Variable is NOT at data[2][0].
                    Variable is NOT at data[2][1].
                    >>>

                    Comment

                    Working...