You can use the str.split method.
>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']
If you want to convert it to a tuple, just
>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')
If you are looking to append to a list, try this:
>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']
Answer from Matt Williamson on Stack OverflowYou can use the str.split method.
>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']
If you want to convert it to a tuple, just
>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')
If you are looking to append to a list, try this:
>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']
In the case of integers that are included at the string, if you want to avoid casting them to int individually you can do:
mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
It is called list comprehension, and it is based on set builder notation.
ex:
>>> mStr = "1,A,B,3,4"
>>> mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
>>> mList
>>> [1,'A','B',3,4]
line.decode() returns a string (a str), not a list. So you are getting back a single entity, not 288 values.
Python has a method associated with strings that will take a string and split it into component parts. Calling the .split() method and giving it the substring to split upon, in this case a ',' should do the trick.
#Read the line of data from the sensor
line = sensor.readline()
#Decode the line to UTF-8 and print it
lineDecoded = line.decode("UTF-8")
values = [int(i) for i in lineDecoded.split(',')] # <<< this should work
# added a list
# comprehension to
# convert values to integers
x = range(1,289) # <<< this is preferred if you need
# a range of numbers from 1 to 288
y = values
#Plot the data
pg.plot(x, y)
NOTE: as @umutto mentions in a comment above, for plotting purposes, there should be no need to convert the values to a numpy array. A list should do just fine.
But, if for some reason you find that you want/need an array:
y = np.array(values)
Use the str.split method for converting the line to a list of strings. Also as @Alien suggests, you should use the range function for getting x, instead of enumerating all the values manually.
y = [int(i) for i in lineDecoded.split(',')]
x = range(1,289)
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(x, lineDecoded)
fig.show()
location_in = 'London, Greater London, England, United Kingdom'
locations = location_in.split(', ')
location_out = [', '.join(locations[n:]) for n in range(len(locations))]
Here's a working one:
location_in = 'London, Greater London, England, United Kingdom'
loci = location_is.spilt(', ') # ['London', 'Greater London',..]
location_out = []
while loci:
location_out.append(", ".join(loci))
loci = loci[1:] # cut off the first element
# done
print location_out
const array = str.split(',');
MDN reference, mostly helpful for the possibly unexpected behavior of the limit parameter. (Hint: "a,b,c".split(",", 2) comes out to ["a", "b"], not ["a", "b,c"].)
Watch out if you are aiming at integers, like 1,2,3,4,5. If you intend to use the elements of your array as integers and not as strings after splitting the string, consider converting them into such.
var str = "1,2,3,4,5,6";
var temp = new Array();
// This will return an array with strings "1", "2", etc.
temp = str.split(",");
Adding a loop like this,
for (a in temp ) {
temp[a] = parseInt(temp[a], 10); // Explicitly include base as per Álvaro's comment
}
will return an array containing integers, and not strings.