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
9 3118 bvdet 2,851
Recognized Expert Moderator Specialist
Is the 110 in the list an integer? - >>> alist = [110]
-
>>> astring = '110'
-
>>> alist[0] == astring
-
False
-
>>> str(alist[0]) == astring
-
True
-
>>> alist == [int(astring)]
-
True
-
>>>
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
bvdet 2,851
Recognized Expert Moderator Specialist
Yes. - 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(slist)
-
else:
-
print 'Variable is not in the list.'
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?
bvdet 2,851
Recognized Expert Moderator Specialist
You could do something like this: - 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(dataList):
-
if s in item:
-
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. - >>> "The %(car)s was clocked at %(speed)d MPH." % {'car': "Lamborghini", 'speed': 220}
-
'The Lamborghini was clocked at 220 MPH'
-
>>> "The %s was clocked at %d MPH." % ("Lamborghini", 220)
-
'The Lamborghini was clocked at 220 MPH.'
-
>>>
Oh Thanks! this is what i got -
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
bvdet 2,851
Recognized Expert Moderator Specialist
Either the variable is in the sublist (row) or not. - 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: - >>> Variable is at data[0][1].
-
Variable is at data[1][1].
-
Variable is not in row 2.
-
>>>
so u cant show every specific spot if it is similiar or not?
bvdet 2,851
Recognized Expert Moderator Specialist
Yes, I suppose so. I don't understand what this will do for you though. - 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: -
>>> 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].
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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......
|
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...
|
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....
| |
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...
|
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...
|
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
...
|
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!
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |