Home Python - How can i remove a letter from a list of strings, change the strings to floats, transform some of the floats, and keep the order

# Python - How can i remove a letter from a list of strings, change the strings to floats, transform some of the floats, and keep the order

Ian Daily
1#
Ian Daily Published in 2018-02-13 05:25:57Z
 myList=['57.43 N', '44.78 S', '59.64 S', '88.11 N']  I'm learning python, I have a list of strings representing Lat/Lon. I need to remove the N/S and when there is an S multiply by -1, and have all strings converted to floats. I was thinking I could build an index of my values, and somehow separate the strings with S from the ones with N, create North and South lists remove the N/S with, x=[x.remove('NS') for x in myList] y=[y.remove('S') for x in myList]  multiply the y list by -1 and use extend x by y so my index stays in tact. Any advice would be very lovely. Thanks.
tverghis
2#
 You can use list comprehensions to achieve this succinctly: newList = [-1 * float(x[:-2]) if 'S' in x else float(x[:-2]) for x in mylist]  Of course, this assumes the following about each of your elements: The last two characters are always ' N' or ' S'. The other characters can be converted into floats.
 You can just split each string into number and direction. If the direction is S, multiply it by -1: myList=['57.43 N', '44.78 S', '59.64 S', '88.11 N'] result = [] for data in myList: num, direction = data.split() num = float(num) if direction == 'S': num *= -1 result.append(num) print(result) # [57.43, -44.78, -59.64, 88.11]