简体   繁体   English

如何根据python3中的元素序列在列表中创建子列表?

[英]How to create sublists within a list based on a sequence of elements in python3?

I have a hex data set and it's pulled into a list called hex_data.我有一个十六进制数据集,它被放入一个名为 hex_data 的列表中。 I want to split the hex_data list into sublists based on a certain repeating sequence.我想根据某个重复序列将 hex_data 列表拆分为子列表。 For ex.例如。 if " 'F5', '59', '9A', 'A5', '58', '89' " is seen within the main list hex_data, I want to split it at 2 elements before the sequence begins again and keep doing the same for the rest of list.如果在主列表 hex_data 中看到“'F5'、'59'、'9A'、'A5'、'58'、'89'”,我想在序列再次开始之前将其拆分为 2 个元素并继续执行列表的其余部分相同。 Please know that I'm extremely new to Python and just can't seem to get out of this issue.请知道我对 Python 非常陌生,似乎无法摆脱这个问题。

Here's what I have so far:这是我到目前为止所拥有的:

   sequence_of_ele = hex_data[2:8]          #'F5', '59', '9A', 'A5', '58', '89'
   for i in range(3, len(hex_data)):        # starting from 3 so it doesn't create an empty sublist before the hex_data list starts
        if hex_data[i:i+6] == sequence_of_ele:
             hex_data = do_split(hex_data, [i-2])
   print(hex_data)

and the above uses the function below that I found on here (but it doesn't iterate throughout the list, it just splits once and the rest of the list is in one big sublist:上面使用了我在这里找到的下面的函数(但它不会遍历整个列表,它只是拆分一次,列表的其余部分在一个大子列表中:

def do_split(lst, slices):
    return [sl.tolist()for sl in np.split(lst, slices)]

OUTPUT:输出:

[['B1', '1F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C2', '2A', 'A8', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'FA'], ['A9', '9F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DF', 'FA', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C1', '18', '88', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'F6', '64', '4F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '89', '90', '0A', 'AF', 'FF', 'F3', '3F', 'FB', 'B0', '0F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DB', 'BA', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '88', '80', '0A', 'AF', 'FF', 'F3', '3F', 'F']]

EXPECTED OUTPUT:预期产出:

[
['B1', '1F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C2', '2A', 'A8', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'FA'], 
['A9', '9F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DF', 'FA', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C1', '18', '88', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'F6'], 
['64','4F', 'F5', '59', '9A', 'A5', '58', '89','9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '89', '90', '0A', 'AF', 'FF', 'F3', '3F', 'FB'],
['B0', '0F', 'F5', '59', '9A', 'A5', '58', '89','9C', 'CB', 'B6', '6D', 'DD', 'DB', 'BA', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '88', '80', '0A', 'AF', 'FF', 'F3', '3F', 'FF']
]

Your code splits the input once, leaving a sublist that is to be split again with the same procedure.您的代码将输入拆分一次,留下一个子列表,该子列表将使用相同的过程再次拆分。 So you can convert your code into a recursive function;所以你可以把你的代码转换成递归函数; ie, make it call itself to process the remaining sublist.即,让它调用自己来处理剩余的子列表。

hex_data = ['B1', '1F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C2', '2A', 'A8', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'FA', 'A9', '9F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DF', 'FA', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C1', '18', '88', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'F6', '64', '4F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '89', '90', '0A', 'AF', 'FF', 'F3', '3F', 'FB', 'B0', '0F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DB', 'BA', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '88', '80', '0A', 'AF', 'FF', 'F3', '3F', 'FF']
sequence_of_ele = hex_data[2:8] # 'F5', '59', '9A', 'A5', '58', '89'

def do_split(lst):
    for i in range(3, len(lst)):
        if lst[i:i+6] == sequence_of_ele:
            return [lst[:i-2]] + do_split(lst[i-2:])
    return [lst]

print(do_split(hex_data))
# [
#   ['B1', '1F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C2', '2A', 'A8', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'FA'],
#   ['A9', '9F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DF', 'FA', 'A6', '66', '60', '0D', 'D3', '30', '00', '0C', 'C1', '18', '88', '8B', 'B0', '0A', 'AF', 'FF', 'F3', '3F', 'F6'],
#   ['64', '4F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'D7', '7A', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '89', '90', '0A', 'AF', 'FF', 'F3', '3F', 'FB'],
#   ['B0', '0F', 'F5', '59', '9A', 'A5', '58', '89', '9C', 'CB', 'B6', '6D', 'DD', 'DB', 'BA', 'A6', '67', '70', '0D', 'D3', '30', '00', '0C', 'C1', '1E', 'E8', '88', '80', '0A', 'AF', 'FF', 'F3', '3F', 'FF']
# ]

If your data is huge, however, recursion would probably fail.但是,如果您的数据很大,递归可能会失败。 In this case, you can resort to a for loop as follows:在这种情况下,您可以使用 for 循环,如下所示:

output = []
bucket = [] # temporary sublist

for i, x in enumerate(hex_data):
    if hex_data[i+2:i+8] == sequence_of_ele:
        if i > 0: # don't do the following for hex_data[0]
            output.append(bucket) # flush; i.e., move bucket to output
            bucket = [] # empty the bucket
    bucket.append(x) # add the current item to the bucket
output.append(bucket) # for the last time, move bucket to output

print(output)

The idea is to keep adding items to a "bucket" until you recognize the pattern, at which point you move the bucket to the output list and prepare a new bucket.这个想法是不断向“存储桶”添加项目,直到您识别出模式,此时您将存储桶移动到输出列表并准备一个新的存储桶。

暂无
暂无

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

相关问题 如何根据开始和结束元素从列表创建子列表? - How to create sublists from list based on start and end elements? 在python的子列表中创建子列表 - create sublists within sublists in python 如何在 Python 中创建子列表列表? - How to create a list of sublists in Python? Python - 如何根据字符串的一部分从字符串列表中创建子列表? - Python - How to create sublists from list of strings based on part of the string? 如何基于索引添加子列表的元素-Python - How to add the elements of sublists based on index - Python 如何首先基于初始列表的单个元素将列表拆分为子列表,然后仅在 python 中将列表的连续部分拆分为子列表? - How to split a list into sublists based on single elements of the initial list first and then contiguous portions of the list simply in python? 如何根据原始列表中与设置值不同的元素对列表进行切片(或创建子列表) - How to slice a list (or create sublists) based on elements in original list that differ by a set value Python如何根据日期创建子列表? - Python how create sublists based by date? Python 根据列表元素第一个数字的相等性将列表分成子列表 - Python list into sublists based on the equality of first digit of the list elements 如何基于python嵌套列表中的公共元素计算子列表的数量? - How to count the number of sublists based on common elements from a nested list in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM