473,508 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help converting a list into a string or vice versa

10 New Member
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
9 3118
bvdet
2,851 Recognized Expert Moderator Specialist
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
victory2006
10 New Member
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
2,851 Recognized Expert Moderator Specialist
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
victory2006
10 New Member
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 Recognized Expert Moderator Specialist
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
victory2006
10 New Member
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 Recognized Expert Moderator Specialist
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
victory2006
10 New Member
so u cant show every specific spot if it is similiar or not?
Dec 1 '08 #9
bvdet
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

0
1896
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
19
22702
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
7
2399
by: Jus! | last post by:
Hi. I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's? eg. File contains: 110001 010011 etc......
5
3316
by: bob | last post by:
Hi Using 2003 - targeting the compact framework (c#), but would like to do most development using the full.net (manually leaving out stuff not in the compact framework). Q. Trying to find a...
1
1704
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
12
2664
by: steven acer | last post by:
hello, i have a java app that constructs an xml from a specific file format and vice versa. i've been asked to convert it to c++, but im not an expert in c++, actually im mere beginner you can...
1
2585
by: Astan Chee | last post by:
Hi, I have a string in this format "DD/MM/YYY" for example: tdate = "18/01/1990" and Im trying to convert this to epoch time, can anyone help? Im also trying to convert that epoch time to the...
6
14756
by: Patrick C | last post by:
If I have a loop that yields something like: returnedlist = how can I make the contents of returnedlist into a string called NewString that outputs something like NewString= (a b c ...
7
6758
by: victory2006 | last post by:
like we have an integer value of : 9 and i need to convert it to binary numbers and vice versa, thanks!
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.