简体   繁体   English

如何根据元素中的第一个数字拆分子列表中的值列表?

[英]How to split the list of values in a sublist based on the first number in a element?

I have a list which looks like:我有一个看起来像的列表:

lines = 
['1 22698U 93041A   08176.51164820  .00000248  00000-', 
'2 22698 089.5192 046.4765 0091800 046.2114 314.6604', 
'1 22698U 93041A   08176.93361871 +.00000248 +00000121', 
'2 22698 089.5191 046.4531 0091744 044.8831 315.9716',
'1 22698U 93041A   08177.84788861  .00000094  00000-',
'2 22698 089.5188 046.4030 0091715 041.9519 318.8655']

I would like to split lines into 3 sublists like:我想将lines拆分为 3 个子列表,例如:

sublist_1 =
 ['1 22698U 93041A   08176.51164820  .00000248  00000-', 
 '2 22698 089.5192 046.4765 0091800 046.2114 314.6604']
 sublist_2 = 
 ['1 22698U 93041A   08176.93361871 +.00000248 +00000121', 
'2 22698 089.5191 046.4531 0091744 044.8831 315.9716',]
 sublist_3 =
 ['1 22698U 93041A   08177.84788861  .00000094  00000-',
'2 22698 089.5188 046.4030 0091715 041.9519 318.8655']

The splitting should be based on the first number of a value in a list.拆分应基于列表中值的第一个数字。 Like sublist should contain one value from a sequence that starts with '1' and another that starts with '2' Like sublist 应该包含一个以“1”开头的序列和另一个以“2”开头的值

I will appreciate any suggestions on how can I manage it.我将不胜感激有关如何管理它的任何建议。

Use numpy.reshape()使用 numpy.reshape()

import numpy as np
lines = np.array(lines)
lines = lines.reshape(-1, 2)

lines[0], lines[1] and lines[2] are your sublists respectively. lines[0]、lines[1] 和 lines[2] 分别是您的子列表。

sublist_1, sublist_2, sublist_3 = lines

You can do it by list comprehension, enumerate and list slicing like你可以通过列表理解、枚举和列表切片来做到这一点

lines =['1 22698U 93041A   08176.51164820  .00000248  00000-',
'2 22698 089.5192 046.4765 0091800 046.2114 314.6604',
'1 22698U 93041A   08176.93361871 +.00000248 +00000121',
'2 22698 089.5191 046.4531 0091744 044.8831 315.9716',
'1 22698U 93041A   08177.84788861  .00000094  00000-',
'2 22698 089.5188 046.4030 0091715 041.9519 318.8655']

sublist_1, sublist_2, sublist_3 = [[v, lines[i*2+1]] for i, v in enumerate(lines[::2])]

OR with zip或与 zip

sublist_1, sublist_2, sublist_3 = (list(v) for v in zip(lines[::2], lines[1::2]))

暂无
暂无

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

相关问题 根据子列表的第一个元素拆分子列表列表 - split a list of sublist based on their first elements 如何通过嵌套列表中第一个元素的值提取子列表 - How to extract a sublist by the value of the first element from a nested list 如何从列表中提取子列表,其中前一个元素的最后一个字符与最后一个元素的第一个字符相同? - 蟒蛇 - How to extract a sublist from a list where the last character of the previous element is the same as the first character of the last element? - python Python:如何检查列表中每个子列表中的第一项是否是连续数字,第二个数字是否相同? - Python: How to check if the first item in each sublist in a list is a consecutive number and the second number is the same? 如何将列表列表组合成字典,其中第一个子列表映射到所有后续子列表中的相应值? - How to combine a list of lists into a dictionary where the first sublist is mapped to corresponding values in all subsequent sublists? 以有效的方式从子列表列表中删除第一个元素 [python 3] - remove first element from a list of sublist in an efficient way [python 3] 使用python将子列表中的第一个元素与同一列表中的元素合并 - Merging first element in sublist with elements in the same list using python 获取字符串子列表的第一个元素作为字典键,其中包含 python 中的值列表 - Get first element of a string sublist as a dictionary key with a list of value in python 如何将字符串列表拆分为len(“”。join(sublist))&lt;= N的子列表? - How to split list of strings into sublists where len(“”.join(sublist)) <= N? 如何根据指定的字符将列表值拆分为字典? - How to split list values into dictionary based on specified character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM