在python中拆分列表中的元素

时间:2017-03-23 20:17:30

标签: python list

我的列表看起来像这样:

[['m', '-407.45833,31.660713', 
'55.94048,-96.761902', 
'136.07143,106.589284', 'z'], 
['m', '-136.82738,39.976189', 
'35.52976,-120.196426', 
'43.089287,124.732141']]

并希望它看起来像这样:

[['m', '-407.45833', '31.660713',
 '55.94048', '-96.761902',
 '136.07143', '106.589284', 'z'],
 ['m', '-136.82738', '39.976189', 
'35.52976', '-120.196426',
 '43.089287', '124.732141']]

有什么建议吗? 编辑:这是我到目前为止所尝试的。

list2=[]
for i in range (0, len(list)):
    list2.append(list[i].split())
list3=[]
for j in range(0, len(list2)):
    for i in range(0, len(list2[j])):
        list3.extend(list[j][i].split(',',1))

4 个答案:

答案 0 :(得分:0)

你的尝试在很多方面都是错误的

  • 在第一次迭代中,您有一个列表列表,因此您无法在列表中使用split
  • 不要将list用作变量名称
  • 不要使用range和索引进行迭代。只需使用for item in the_list:
  • 即可

我,我会跳过所有这些并使用列表推导:

根据,进行拆分,并使用双列表理解将结果列表展平,包含在外部列表解析中以迭代列表列表:

l = [['m', '-407.45833,31.660713', '55.94048,-96.761902', '136.07143,106.589284', 'z'], ['m', '-136.82738,39.976189', '35.52976,-120.196426', '43.089287,124.732141']]

l = [[x for s in sl for x in s.split(',')] for sl in l]

print(l)

结果:

[['m', '-407.45833', '31.660713', '55.94048', '-96.761902', '136.07143', '106.589284', 'z'], ['m', '-136.82738', '39.976189', '35.52976', '-120.196426', '43.089287', '124.732141']]

答案 1 :(得分:0)

使用str.split()str.join()函数的简单解决方案:

l = [['m', '-407.45833,31.660713', '55.94048,-96.761902', '136.07143,106.589284', 'z'], ['m', '-136.82738,39.976189', '35.52976,-120.196426', '43.089287,124.732141']]
result = [','.join(i).split(',') for i in l]

print(result)

输出:

[['m', '-407.45833', '31.660713', '55.94048', '-96.761902', '136.07143', '106.589284', 'z'], ['m', '-136.82738', '39.976189', '35.52976', '-120.196426', '43.089287', '124.732141']]

答案 2 :(得分:0)

鉴于:

>>> data = [['m', '-407.45833,31.660713', '55.94048,-96.761902', '136.07143,106.589284', 'z'], 
            ['m', '-136.82738,39.976189', '35.52976,-120.196426', '43.089287,124.732141']]

关键是在新的结果列表中创建一个新的 out_group 。要填充 out_group ,请循环输入,然后遍历其拆分字段。这会使数据结构“扁平化”:

>>> result = []
>>> for group in data:
        out_group = []
        for element in group:
            for subelement in element.split(','):
                out_group.append(subelement)
        result.append(out_group)

>>> result
[['m', '-407.45833', '31.660713', '55.94048', '-96.761902', '136.07143', '106.589284', 'z'],
 ['m', '-136.82738', '39.976189', '35.52976', '-120.196426', '43.089287', '124.732141']]

答案 3 :(得分:0)

这是一个简单的必要方法。

def split_up_elements(outer_list):
    results = []
    for inner_list in outer_list:
        temp_inner_list = []
        for item in inner_list:
            temp_inner_list.extend(item.split(','))
        results.append(temp_inner_list)
    return results

示例输出

>>> outer_list = [['m', '-407.45833,31.660713', '55.94048,-96.761902', '136.07143,106.589284', 'z'], ['m', '-136.82738,39.976189', '35.52976,-120.196426', '43.089287,124.732141']]
>>> split_up_elements(outer_list)
[['m', '-407.45833', '31.660713', '55.94048', '-96.761902', '136.07143', '106.589284', 'z'], ['m', '-136.82738', '39.976189', '35.52976', '-120.196426', '43.089287', '124.732141']]