简体   繁体   中英

Split a List of Strings - to get first and last element

For the list 'wind'

wind=['', 'W at 6kph', 'SSE at 14kph', 'SSE at 23kph', 'WSW at 28kph', 'WSW at 15kph', 'S at 9kph', 'SW at 18kph', 'NNW at 6kph']

I would like to split this into 2 lists.

Desired result:

wind_direction=['','W','SSE','SSE','WSW','WSW','S','SW','NNW']
wind_strength=[6,14,23,28,15,9,18,6]

My attempt:

wind_direction=[(y.split(' '))[0] for y in wind]
print(wind_direction)

wind_strength=[(x.split(' '))[2] for x in wind]
print(wind_strength)

The error for wind_strength is 'list index out of range'

Thankyou

wind=['', 'W at 6kph', 'SSE at 14kph', 'SSE at 23kph', 'WSW at 28kph', 'WSW at 15kph', 'S at 9kph', 'SW at 18kph', 'NNW at 6kph']
direction, speed = zip(*[item.split(' at ') for item in wind if item])
print(direction)
print(speed)

# use ONE of the next 2 lines if you want to remove kph and convert to int
speed = [int(item.removesuffix('kph')) for item in speed] # in python before 3.9 use rstrip instead of removesuffix
# OR
speed = [int(item[:-3]) for item in speed]
print(speed)

output

('W', 'SSE', 'SSE', 'WSW', 'WSW', 'S', 'SW', 'NNW')
('6kph', '14kph', '23kph', '28kph', '15kph', '9kph', '18kph', '6kph')
[6, 14, 23, 28, 15, 9, 18, 6]

The solution for the exact question:

wind=['', 'W at 6kph', 'SSE at 14kph', 'SSE at 23kph', 'WSW at 28kph', 'WSW at 15kph', 'S at 9kph', 'SW at 18kph', 'NNW at 6kph']

wind = wind[1:] # removing the ''

wind_direction=[w.split(' at ')[0] for w in wind]
wind_strength=[w.split(' at ')[1].removesuffix('kph') for w in wind]

wind_direction.insert(0, '') # adding the '' back to 1 of the lists

I dont think your question is right, since the data isn't matching:

['','W','SSE','SSE','WSW','WSW','S','SW','NNW'] # len is 9
[6,14,23,28,15,9,18,6] # len is 8

In case you want to remove the '' from the list , and then proccess:

wind=['', 'W at 6kph', 'SSE at 14kph', 'SSE at 23kph', 'WSW at 28kph', 'WSW at 15kph', 'S at 9kph', 'SW at 18kph', 'NNW at 6kph']

wind = wind[1:] # removing the ''

wind_direction=[w.split(' at ')[0] for w in wind]
wind_strength=[w.split(' at ')[1].removesuffix('kph') for w in wind]

Using list comprehension

wind_direction=[(y.split(' '))[0] for y in wind]
print(wind_direction)

wind_strength=[int((x.split(' ')[2]).split('kph')[0])  for x in wind if x != '']
print(wind_strength)

Output

['', 'W', 'SSE', 'SSE', 'WSW', 'WSW', 'S', 'SW', 'NNW']
[6, 14, 23, 28, 15, 9, 18, 6]

For python3.9+

wind_direction: list[str] = []
wind_strength: list[int] = []

for s in wind:
    if s == '':
        wind_direction.append(s)
        continue
    items = s.split()
    wind_direction.append(items[0])
    if v := ''.join(i for i in items[2] if i.isdigit()):
        wind_strength.append(int(v))
wind_direction=[]
wind_strength=[]
for z in wind:
    if z=='':
        wind_direction.append('')
        wind_strength.append('')
    else:
        wind_direction.append(z.split(' ')[0])
        wind_strength.append(int(z.split(' ')[2].split('kph')[0]))
print(wind_direction)
print(wind_strength)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM