0

How do I get datalist to string?

>>> days = 24/6
>>> print days
4
>>> hours = 45/7
>>> print hours
6
>>> datalist = []
>>> datalist.append(days)
>>> datalist.append(" days ")
>>> print datalist
[4, ' days ']
>>> datalist_str = "".join(datalist)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: sequence item 0: expected string, int found

6
Contributors
8
Replies
14
Views
9 Years
Discussion Span
Last Post by cementovoz
0

Pretty easy, I came up with this one liner for it:

print " ".join(["%s" % el for el in datalist])

Hope that helps. : D

0
  1. print " ".join(["%s" % el for el in datalist])

This means, take take el from datalist, convert it to string "%s" % el, and put into the list.
Correct me if I am wrong.

0

How do I pass the strings in a list to separate variables ?
For example:

short_list =

I want the following variables to store the individual short_list values as shown:

var_a = 'a'
var_b = 'b'
var_c = 'c'

Thanks!
iamnew

0

Next time please push start a new thread button from the Python groups main list so your post will be better seen.

I push this (code) here, that gives nice looking fixed width text and keeps the white space, which is so essential for Python. Use also that next time when you some code stuff.

This is from interactive Python prompt as your request was so simple.

>>> short_list = ['a\n' , 'b\n', 'c\n']
>>> var_a, var_b, var_c = [ x.rstrip() for x in short_list ] ## rstrip newline out and assign
>>> var_a, var_b, var_c
('a', 'b', 'c')
>>> 

Edited by mike_2000_17: Fixed formatting

0

It's a trick question because you don't store the values in a separate variable (that's why we have lists), you use
short_list[0].strip()
short_list[1].strip()
short_list[2].strip()
or strip all of the values in short_list if you don't want to strip() each time.

short_list = ['a\n' , 'b\n', 'c\n']
for x in range(len(short_list)):
   short_list[x] = short_list[x].strip()

Edited by woooee: n/a

0

Thank you! This exactly the way I wanted, I can now proceed with further scripting, :).

Next time please push start a new thread button from the Python groups main list so your post will be better seen.

I push this (code) here, that gives nice looking fixed width text and keeps the white space, which is so essential for Python. Use also that next time when you some code stuff.

This is from interactive Python prompt as your request was so simple.

>>> short_list = ['a\n' , 'b\n', 'c\n']
>>> var_a, var_b, var_c = [ x.rstrip() for x in short_list ] ## rstrip newline out and assign
>>> var_a, var_b, var_c
('a', 'b', 'c')
>>>

end quote.

Edited by mike_2000_17: Fixed formatting

0

You are welcome but consider that generally it is not so much necessary put list
elements to different variables if you use lists properly.

Consider therefore also this using [index] to get the elements. In that case be careful in that index goes from 0 until length-1.

short_list = [ x.rstrip() for x in short_list ] ## rstrip newline out

Edited by pyTony: Hijacked thread, only original poster can close

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.