By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

need help converting a list into a string or vice versa

P: 10
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?

thanks in advance
Nov 28 '08 #1
Share this Question
Share on Google+
9 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 10
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
Nov 28 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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

P: 10
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
Expert Mod 2.5K+
P: 2,851
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

P: 10
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
Expert Mod 2.5K+
P: 2,851
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

P: 10
so u cant show every specific spot if it is similiar or not?
Dec 1 '08 #9

bvdet
Expert Mod 2.5K+
P: 2,851
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

Post your reply

Sign in to post your reply or Sign up for a free account.