简体   繁体   English

拆分字符串列表 - 获取第一个和最后一个元素

[英]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.我想把它分成2个列表。

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' wind_strength 的错误是“列表索引超出范围”

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+对于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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫数据框拆分并获取列表的最后一个元素 - pandas dataframe split and get last element of list 根据拆分的第一个元素对字符串列表进行排序(日期时间) - Sorting a list of strings based on the first element from split (datetime) 在Python中,可以在一个表达式中的第一个,内部和最后一个元素中拆分列表吗? - In Python is it possible to split a list in first, inner and last element in one expression? 如何将字符串拆分为名字和姓氏 - how to split the strings into first name and last name 如何将列表中的第一个和最后一个元素相加以获得最后的 2 位数字? - How to sum first and last element in list to get 2 digit number in the end? 使用 SQLAlchemy 获取第一个和最后一个元素 - Get first AND last element with SQLAlchemy 特定元素列表中的正则表达式/分割字符串 - Regex/split strings in list for particular element 更改列表列表中的所有字符串,但最后一个元素 - Change all strings in list of lists but the last element 检查第一个字符串列表中的元素是否出现在第二个字符串列表中 - Checking if an element in first list of strings occurs in second list of strings 列表的第一个元素和最后一个元素,Python - First element and last ones of list, Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM