在python3中,如何在特定位置拆分列表列表?

时间:2018-05-13 22:31:33

标签: python python-3.x list split

['2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA']  
['2006/10/18 10:08:45 32.165 -115.891 7.3 BAJA CALIFORNIA, MEXICO']  

同一原始列表中有两个列表:

a = [['2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA'], 
['2006/10/18 10:08:45 32.165 -115.891 7.3 BAJA CALIFORNIA, MEXICO'] ] 

在这里,我想在国家/地区名称之前拆分它们,例如:

['2006/10/18 21:15:51 4.823 -82.592 37.3]' '[SOUTH OF PANAMA']  
['2006/10/18 10:08:45 32.165 -115.891 7.3]' '[BAJA CALIFORNIA, MEXICO']  

我的问题是如何使用split()(或者其他一些方法)让python理解我想在这个特定位置分离这些列表?

3 个答案:

答案 0 :(得分:2)

您可以使用ALTER TABLE users_users ADD FULLTEXT(firstname, lastname, link); $searchTerm = '+'.str_replace(' ','+',$search).'*'; $query = "SELECT * FROM users where MATCH(`firstname`,`lastname`,`link`) AGAINST (".searchTerm." IN BOOLEAN MODE)";

re.split

输出:

import re
s = [['2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA']  , ['2006/10/18 10:08:45 32.165 -115.891 7.3 BAJA CALIFORNIA, MEXICO']]
new_s = [re.split('(?<=\d)\s(?=[A-Z])', i) for [i] in s]

答案 1 :(得分:2)

a = [['2006/10/18 21:15:51 4.823 -82.592 37.3 SOUTH OF PANAMA'], 
['2006/10/18 10:08:45 32.165 -115.891 7.3 BAJA CALIFORNIA, MEXICO'] ]

fixed_split = 5
b = [[' '.join(i[0].split()[:fixed_split]), ' '.join(i[0].split()[fixed_split:])] for i in a]
print(b)

输出

[['2006/10/18 21:15:51 4.823 -82.592 37.3', 'SOUTH OF PANAMA'], ['2006/10/18 10:08:45 32.165 -115.891 7.3', 'BAJA CALIFORNIA, MEXICO']]

答案 2 :(得分:2)

re.split()是复杂分割的不错选择:

import re

[re.split('([\d\/\.\-\:\s]+)\s+([A-Z\s\,]+)', x[0])[1:3] for x in a]