Convert python list to string [duplicate] - python

This question already has answers here:
Converting a list to a string [duplicate]
(8 answers)
Closed 9 years ago.
I have a python list like so:
list = []
for loop
list.append("blah")
What I want to do it convert this list to string with each value separated by comma.
How do I do this??

Use the str.join method:
','.join(your_list)

Related

How to return more elements within a list as a string in Python [duplicate]

This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 1 year ago.
I have following elements within a list b['lemma'] within a function:
babicka
Marta
I want to return them as string like:
print(b['lemma'], end=" ")
>> babicka Marta
Help would be appreciated.
Use the string join() method, like this:
return " ".join(b['lemma'])

How to sort a list of numbers that with fixed prefix? [duplicate]

This question already has answers here:
Sort list of strings by integer suffix
(4 answers)
Is there a built in function for string natural sort?
(23 answers)
Closed 1 year ago.
I have a list of ['rs12345','rs21','rs55189'];
I need to sort them as numbers after strip the prefix 'rs'.
How can I do it in Python ?
# row.keys() is ['rs12345','rs21','rs55189']
fieldnames = sorted(list(row.keys()),key=itemgetter(slice(2, None)))
This code will not working after add int(''.join(xxx)).
And the dict row is a generator so I have to put it into list() to get its values.
_fieldnames = list(row.keys())
_fieldnames.remove(sidname)
_fieldnames = sorted(_fieldnames, key=lambda i: int(i[2:]))
Got it working.
I forgot that I have to remove sampleid, which contains no ^rs, first.

Split list element into lists in python [duplicate]

This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.

How to convert a string of a list into a list in Python? [duplicate]

This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 4 years ago.
I have a text file filled with lines like this:
[[19.305199999999999, -126.56959999999999], [19.445499999999999, -126.46599999999999], [19.196999999999999, -126.396], [19.130700000000001, -126.65900000000001], [19.378900000000002, -126.73]]
When I read these lines, they are interpreted as strings. Is there any way I can read them as lists or arrays or easily convert them into lists or arrays?
Use the ast module
Ex:
import ast
l = "[[19.305199999999999, -126.56959999999999], [19.445499999999999, -126.46599999999999], [19.196999999999999, -126.396], [19.130700000000001, -126.65900000000001], [19.378900000000002, -126.73]]"
print(ast.literal_eval(l))
Output:
[[19.3052, -126.5696], [19.4455, -126.466], [19.197, -126.396], [19.1307, -126.659], [19.3789, -126.73]]

String to Int in Python [duplicate]

This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s

Categories