猿问

通过正则表达式捕获组,然后将捕获组拆分为单独的列表项

我已将行读入列表:


l =  ['W –-Transportation',

     'W23.F5-International_waterways                      W25.2-Airlines',

     'W23.F8-Rivers                                       W25.4-Bus_lines',

     'W23.H-Pipelines                                       W25.6-Railroads',

     'W23.H2-Oil_pipelines                                W25.8-Shipping_lines',

     'W23.H4-Natural_gas_pipelines                        W27-Transportation_safety',

     'W23.H6-Water_pipelines                              W27.2-Traffic_safety',

     'W23.K-Transportation_system_design                    W29-Navigation',

     'W23.M-Transportation_system_construction              W32-Transportation_research',

     'W23.M2-Transportation_facility_construction         W32.2-Transportation_surveys',

     'W23.M4-Transportation_system_maintenance            W34-Transportation_education',

     'W23.M4.2-Road_maintenance                        W36-Transportation_policy',

     'W23.M6-Transportation_system_repair                 W38-Transportation_planning',

     'W23.M6.2-Vehicle_repair                          W40-Transportation_aspects',

     'W25-Transportation_industry']

现在对于每一行,我想捕获 2 个组,例如W23.F5-International_waterways,W25.2-Airlines并将它们分成两个列表条目。


我的预期结果是:


l =  ['W –-Transportation','W23.F5-International_waterways','W25.2-Airlines','W23.F8-Rivers','W25.4-Bus_lines','W23.H-Pipelines','W25.6-Railroads','W23.H2-Oil_pipelines','W25.8-Shipping_lines', .....,'W25-Transportation_industry']

捕获组的正则表达式是,([a-z])\s*?([A-Z])但我应该如何将捕获组拆分为新的列表条目?


千巷猫影
浏览 111回答 1
1回答

郎朗坤

也许,对 a 的简单拆分"  "可能在这里可以正常工作:import rel =  ['W –-Transportation',     'W23.F5-International_waterways                      W25.2-Airlines',     'W23.F8-Rivers                                       W25.4-Bus_lines',     'W23.H-Pipelines                                       W25.6-Railroads',     'W23.H2-Oil_pipelines                                W25.8-Shipping_lines',     'W23.H4-Natural_gas_pipelines                        W27-Transportation_safety',     'W23.H6-Water_pipelines                              W27.2-Traffic_safety',     'W23.K-Transportation_system_design                    W29-Navigation',     'W23.M-Transportation_system_construction              W32-Transportation_research',     'W23.M2-Transportation_facility_construction         W32.2-Transportation_surveys',     'W23.M4-Transportation_system_maintenance            W34-Transportation_education',     'W23.M4.2-Road_maintenance                        W36-Transportation_policy',     'W23.M6-Transportation_system_repair                 W38-Transportation_planning',     'W23.M6.2-Vehicle_repair                          W40-Transportation_aspects',     'W25-Transportation_industry']k = []for i in l:    new_string = i.split("  ")    for j in new_string:        if j != '':            k.append(j.strip())print(k)输出['W –-Transportation', 'W23.F5-International_waterways', 'W25.2-Airlines', 'W23.F8-Rivers', 'W25.4-Bus_lines', 'W23.H-Pipelines', 'W25.6-Railroads', 'W23.H2-Oil_pipelines', 'W25.8-Shipping_lines', 'W23.H4-Natural_gas_pipelines', 'W27-Transportation_safety', 'W23.H6-Water_pipelines', 'W27.2-Traffic_safety', 'W23.K-Transportation_system_design', 'W29-Navigation', 'W23.M-Transportation_system_construction', 'W32-Transportation_research', 'W23.M2-Transportation_facility_construction', 'W32.2-Transportation_surveys', 'W23.M4-Transportation_system_maintenance', 'W34-Transportation_education', 'W23.M4.2-Road_maintenance', 'W36-Transportation_policy', 'W23.M6-Transportation_system_repair', 'W38-Transportation_planning', 'W23.M6.2-Vehicle_repair', 'W40-Transportation_aspects', 'W25-Transportation_industry']
随时随地看视频慕课网APP

相关分类

Python
我要回答