471,595 Members | 1,092 Online

# 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"(string) 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?

Nov 28 '08 #1
9 2975
bvdet
2,851 Expert Mod 2GB
Is the 110 in the list an integer?
Expand|Select|Wrap|Line Numbers
1. >>> alist = [110]
2. >>> astring = '110'
3. >>> alist[0] == astring
4. False
5. >>> str(alist[0]) == astring
6. True
7. >>> alist == [int(astring)]
8. True
9. >>>
Nov 28 '08 #2

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
Nov 28 '08 #3
bvdet
2,851 Expert Mod 2GB
Yes.
Expand|Select|Wrap|Line Numbers
1. data = [[1,1,0],[0,1,0],[0,0,1]]
2. s = "110"
3.
4. slist = [int(i) for i in list(s)]
5.
6. if slist in data:
7.     print 'Variable is at list index %s.' % data.index(slist)
8. else:
9.     print 'Variable is not in the list.'
Nov 28 '08 #4
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(slist) " those % mean module?
Nov 28 '08 #5
bvdet
2,851 Expert Mod 2GB
You could do something like this:
Expand|Select|Wrap|Line Numbers
1. s = '10'
2. data = [[1,1,0],[0,1,0],[0,0,1]]
3. dataList = [''.join([str(i) for i in item]) for item in data]
4. for i, item in enumerate(dataList):
5.     if s in item:
6.         print 'Variable is at data[%s][%s].' % (i, item.index(s))
The modulo operator (s % d) produces a formatted string given a format string s and a tuple or dictionary d.
Expand|Select|Wrap|Line Numbers
1. >>> "The %(car)s was clocked at %(speed)d MPH." % {'car': "Lamborghini", 'speed': 220}
2. 'The Lamborghini was clocked at 220 MPH'
3. >>> "The %s was clocked at %d MPH." % ("Lamborghini", 220)
4. 'The Lamborghini was clocked at 220 MPH.'
5. >>>
Nov 28 '08 #6
Oh Thanks! this is what i got

Expand|Select|Wrap|Line Numbers
1. Pattern to be searched: 10
2. OK
3.
4. The board is
5.
6.
7. Col1   Col2   Col3
8. Row1   1   0   0
9. Row2   1   1   0
10. Row3   1   1   0
11.
12. [[1, 0, 0], [1, 1, 0], [1, 1, 0]]
13. Variable is at data[0][0].
14. Variable is at data[1][1].
15. Variable is at data[2][1].
16.
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
Nov 28 '08 #7
bvdet
2,851 Expert Mod 2GB
Either the variable is in the sublist (row) or not.
Expand|Select|Wrap|Line Numbers
1. s = '10'
2. dataList = [''.join([str(i) for i in item]) for item in data]
3. for i, item in enumerate(dataList):
4.     if s in item:
5.         print 'Variable is at data[%s][%s].' % (i, item.index(s))
6.     else:
7.         print 'Variable is not in row %s.' % i
Output:
Expand|Select|Wrap|Line Numbers
1. >>> Variable is at data[0][1].
2. Variable is at data[1][1].
3. Variable is not in row 2.
4. >>>
Nov 29 '08 #8
so u cant show every specific spot if it is similiar or not?
Dec 1 '08 #9
bvdet
2,851 Expert Mod 2GB
Yes, I suppose so. I don't understand what this will do for you though.
Expand|Select|Wrap|Line Numbers
1. data = [[1,1,0],[0,1,0],[0,0,1]]
2. s = '10'
3. dataList = [''.join([str(i) for i in item]) for item in data]
4. for i, item in enumerate(dataList):
5.     for j in range(len(item)-len(s)+1):
6.         if s == item[j:j+len(s)]:
7.             print 'Variable is at data[%s][%s].' % (i, j)
8.         else:
9.             print 'Variable is NOT at data[%s][%s].' % (i, j)
Output:
Expand|Select|Wrap|Line Numbers
1. >>> Variable is NOT at data[0][0].
2. Variable is at data[0][1].
3. Variable is NOT at data[1][0].
4. Variable is at data[1][1].
5. Variable is NOT at data[2][0].
6. Variable is NOT at data[2][1].
7. >>>
Dec 1 '08 #10