Convert a list to a list of strings
Hi
I have a list like this: [GsaI, MaeI, Sse9I, MspR9I, SfuI, BssECI, Eco88I, Bsp119I, ...], but I need to convert all of its elements to strings. So, I want a list of strings, like: ["GsaI", "MaeI", "Sse9I", "MspR9I", "SfuI", "BssECI", "Eco88I", "Bsp119I", ...].
How can I do that?
Thank you
• 1,606 views
•
link
1 answer
I have no idea how you got this kind of list, but try to iterate over the list and use the
str(element)
command.
example:
y =1 str(y) returns: '1'
• 0 views
•
link
Log in to answer this question.
I don't follow how you've made python store the value GsaI as something other than a string, could you elaborate please.
Were they integers in a list intVersion, say, you could do strVersion = [str(s) for s in intVersion]
This list is the result of a set, I've converted the set to a list, but if the elements of list aren't string I can't continue the code....
there's no way to convert the elements to strings in the list?
We will help you, but we need to know more. Paste your code or something.
Can you please describe how you built a python Set that contains your elements? I tried making a Set that contains your items, but I can only add them as strings:
$ python Python 2.7.6 (default, Jul 9 2014, 20:49:24) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.38)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from sets import Set >>> REs = Set() >>> REs.add(Eco88I) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'Eco88I' is not defined >>> REs.add('Eco88I') >>> print REs Set(['Eco88I']) >>>Please add a snippet of code and maybe some debug statements that show what you have so far.
Hello albamaria.muniesa!
We believe that this post is does not fit the main topic of this site.
I do not understand how this could be ever related to bioinformatics, even if the strings had a biological meaning. In my view this is a basic python programming question.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
I am closing this post as it doesn't fit this forum. Please google "convert elements in list to string in python" and you will find numerous results.
Hello albamaria.muniesa!
We believe that this post is does not fit the main topic of this site.
Not relevant to this forum
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
Hello albamaria.muniesa!
We believe that this post is does not fit the main topic of this site.
I am closing it again!
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
It seems like one can close a question multiple times!