简体   繁体   中英

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. I want to split the hex_data list into sublists based on a certain repeating sequence. 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. Please know that I'm extremely new to Python and just can't seem to get out of this issue.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM