Reputation: 97
Im trying to split
dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
into list form where if I wanted to get 194.6875
I could do
print(dimensions[8])
Im having trouble converting it to a list since there are multiple types.
Upvotes: 1
Views: 284
Reputation: 54168
You could split on space and convert to float the values that are not alphabetic strings
dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
values = [val if val.isalpha() else float(val) for val in dimensions.split(" ")]
print(values) # ['M', 0.0, 0.0, 'C', 65.0, 1.0, 130.0, 1.0, 194.6875, 0.0, 'C', 195.0, 17.0, 195.0, 33.0, 194.6875, 50.0, 'C', 130.0, 49.0, 65.0, 49.0, 0.0, 50.0, 'C', 0.0, 33.0, 0.0, 17.0, 0.0, 0.0, 'z']
print(values[8], type(values[8])) # 194.6875 <class 'float'>
Upvotes: 1
Reputation: 779
You can use the .split() method on a string and pass in the character you want to split on, i.e:
dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
listDimensions = dimensions.split(' ')
x = float(listDimensions[8])
print(x)
After that it is a case of changing the data type of the item you want with something like str()
, int()
or float()
Upvotes: 1
Reputation: 276
dimensions = "M 0 0 C 65 1 130 1 194.6875 0 C 195 17 195 33 194.6875 50 C 130 49 65 49 0 50 C 0 33 0 17 0 0 z"
dimensions = dimensions.split(" ")
print(float(dimensions[8]))
Upvotes: 1