搜索和拆分列表

yyhrrdl8  于 2021-07-13  发布在  Java
关注(0)|答案(4)|浏览(321)

我试图搜索一个十六进制值的列表,当我看到一个'55'时,将它们拆分成单独的列表。十六进制值列表如下所示

['55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'f0', '05', '91',
 '73', '06', '00', '00', 'ff', 'ff', 'ff', 'ff', '44', '00', 'fd', '55',
 '00', '0a', '07', '01', 'eb', 'd2', '00', '48', 'ff', 'ff', '01', 'a8',
 '90', '6d', '00', '00', 'ff', 'ff', 'ff', 'ff', '38', '00', '3c', '55',
 '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '02', '00', '30',
 '05', '80', 'df', '10', '00', '00', 'ff', 'ff', 'ff', 'ff', '4a', '00',
 '52', '55', '00', '09', '07', '01', '56', 'd2', '40', '02', '40', '05',
 '0b', '23', '48', '00', '00', 'ff', 'ff', 'ff', 'ff', '50', '00', 'c1',
 '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '00', '00', '00',
 '00', '05', '80', 'ba', 'db', '00', '00', 'ff', 'ff', 'ff', 'ff', '47',
 '00', 'd9', '55', '00', '09', '07', '01', '56', 'd2', '40', '01', '30',
 '01', '9a', 'c0', '56', '00', '00', 'ff', 'ff', 'ff', 'ff', '40', '00',
 'f6', '55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'b0', '05',
 '82', 'e1', '76', '00', '00', 'ff', 'ff', 'ff', 'ff', '58', '00', '27',
 '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '03', '00',
 '30', '05', '80', 'df', '27', '00', '00', 'ff', 'ff', 'ff', 'ff', '52',
 '00', 'f8']`.

使用下面的代码,我只得到最后一个列表。

for x in line:
    if x == '55':
        count+=1
        linetwo = x
    else:
        linetwo += x
print(linetwo)

i、 e.输出为 55000c070196d24000200300300580df270000ffffffff5200f8 但我想 ['550009070156d24000f0059173060000ffffffff4400fd', '55000a0701ebd20048ffff01a8906d0000ffffffff38003c'.... 等,待输出

b1payxdu

b1payxdu1#

一个更干净的解决问题的方法可以是这样

def splitList(a):
    combined = "|".join(a)
    splitString = combined.split("55")
    response = [f'55{splitPart.replace("|", "")}' for splitPart in splitString[1:]]
    return response

哪里 a 是以下列表:

['55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'f0', '05', '91', '73', '06', '00', '00', 'ff', 'ff', 'ff', 'ff', '44', '00', 'fd', '55', '00', '0a', '07', '01', 'eb', 'd2', '00', '48', 'ff', 'ff', '01', 'a8', '90', '6d', '00', '00', 'ff', 'ff', 'ff', 'ff', '38', '00', '3c', '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '02', '00', '30', '05', '80', 'df', '10', '00', '00', 'ff', 'ff', 'ff', 'ff', '4a', '00', '52', '55', '00', '09', '07', '01', '56', 'd2', '40', '02', '40', '05', '0b', '23', '48', '00', '00', 'ff', 'ff', 'ff', 'ff', '50', '00', 'c1', '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '00', '00', '00', '00', '05', '80', 'ba', 'db', '00', '00', 'ff', 'ff', 'ff', 'ff', '47', '00', 'd9', '55', '00', '09', '07', '01', '56', 'd2', '40', '01', '30', '01', '9a', 'c0', '56', '00', '00', 'ff', 'ff', 'ff', 'ff', '40', '00', 'f6', '55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'b0', '05', '82', 'e1', '76', '00', '00', 'ff', 'ff', 'ff', 'ff', '58', '00', '27', '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '03', '00', '30', '05', '80', 'df', '27', '00', '00', 'ff', 'ff', 'ff', 'ff', '52', '00', 'f8']

结果:

['550009070156d24000f0059173060000ffffffff4400fd', '55000a0701ebd20048ffff01a8906d0000ffffffff38003c', '55000c070196d24000200200300580df100000ffffffff4a0052', '550009070156d2400240050b23480000ffffffff5000c1', '55000c070196d24000000000000580badb0000ffffffff4700d9', '550009070156d2400130019ac0560000ffffffff4000f6', '550009070156d24000b00582e1760000ffffffff580027', '55000c070196d24000200300300580df270000ffffffff5200f8']
ac1kyiln

ac1kyiln2#

你可以用 itertools.groupby 具体如下:

from itertools import groupby
lst = ['55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'f0', '05', '91',
 '73', '06', '00', '00', 'ff', 'ff', 'ff', 'ff', '44', '00', 'fd', '55',
 '00', '0a', '07', '01', 'eb', 'd2', '00', '48', 'ff', 'ff', '01', 'a8',
 '90', '6d', '00', '00', 'ff', 'ff', 'ff', 'ff', '38', '00', '3c', '55',
 '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '02', '00', '30',
 '05', '80', 'df', '10', '00', '00', 'ff', 'ff', 'ff', 'ff', '4a', '00',
 '52', '55', '00', '09', '07', '01', '56', 'd2', '40', '02', '40', '05',
 '0b', '23', '48', '00', '00', 'ff', 'ff', 'ff', 'ff', '50', '00', 'c1',
 '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '00', '00', '00',
 '00', '05', '80', 'ba', 'db', '00', '00', 'ff', 'ff', 'ff', 'ff', '47',
 '00', 'd9', '55', '00', '09', '07', '01', '56', 'd2', '40', '01', '30',
 '01', '9a', 'c0', '56', '00', '00', 'ff', 'ff', 'ff', 'ff', '40', '00',
 'f6', '55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'b0', '05',
 '82', 'e1', '76', '00', '00', 'ff', 'ff', 'ff', 'ff', '58', '00', '27',
 '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '03', '00',
 '30', '05', '80', 'df', '27', '00', '00', 'ff', 'ff', 'ff', 'ff', '52',
 '00', 'f8']

for key, values in groupby(lst, lambda x: x == '55'):
    if not key:
        output = "55" + "".join(values)
        print(output)

这将产生

550009070156d24000f0059173060000ffffffff4400fd
55000a0701ebd20048ffff01a8906d0000ffffffff38003c
55000c070196d24000200200300580df100000ffffffff4a0052
550009070156d2400240050b23480000ffffffff5000c1
55000c070196d24000000000000580badb0000ffffffff4700d9
550009070156d2400130019ac0560000ffffffff4000f6
550009070156d24000b00582e1760000ffffffff580027
55000c070196d24000200300300580df270000ffffffff5200f8
oyxsuwqo

oyxsuwqo3#

你在重写 linetwo 每次你到达一个新的 '55' ,则应在生成结果时将结果附加到列表中,而不是放弃上一个结果:

line = ['55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'f0', '05', '91', '73', '06', '00', '00', 'ff', 'ff', 'ff', 'ff', '44', '00', 'fd', '55', '00', '0a', '07', '01', 'eb', 'd2', '00', '48', 'ff', 'ff', '01', 'a8', '90', '6d', '00', '00', 'ff', 'ff', 'ff', 'ff', '38', '00', '3c', '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '02', '00', '30', '05', '80', 'df', '10', '00', '00', 'ff', 'ff', 'ff', 'ff', '4a', '00', '52', '55', '00', '09', '07', '01', '56', 'd2', '40', '02', '40', '05', '0b', '23', '48', '00', '00', 'ff', 'ff', 'ff', 'ff', '50', '00', 'c1', '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '00', '00', '00', '00', '05', '80', 'ba', 'db', '00', '00', 'ff', 'ff', 'ff', 'ff', '47', '00', 'd9', '55', '00', '09', '07', '01', '56', 'd2', '40', '01', '30', '01', '9a', 'c0', '56', '00', '00', 'ff', 'ff', 'ff', 'ff', '40', '00', 'f6', '55', '00', '09', '07', '01', '56', 'd2', '40', '00', 'b0', '05', '82', 'e1', '76', '00', '00', 'ff', 'ff', 'ff', 'ff', '58', '00', '27', '55', '00', '0c', '07', '01', '96', 'd2', '40', '00', '20', '03', '00', '30', '05', '80', 'df', '27', '00', '00', 'ff', 'ff', 'ff', 'ff', '52', '00', 'f8']

out = []
linetwo = ""
count = 0
for x in line:
    if x == '55':
        count+=1
        if linetwo:
            out += [linetwo]
        linetwo = x
    else:
        linetwo += x
out += [linetwo]
print(out)

# ['550009070156d24000f0059173060000ffffffff4400fd', '55000a0701ebd20048ffff01a8906d0000ffffffff38003c', '55000c070196d24000200200300580df100000ffffffff4a0052', '550009070156d2400240050b23480000ffffffff5000c1', '55000c070196d24000000000000580badb0000ffffffff4700d9', '550009070156d2400130019ac0560000ffffffff4000f6', '550009070156d24000b00582e1760000ffffffff580027', '55000c070196d24000200300300580df270000ffffffff5200f8']

或者你可以使用 join , split 以及 map :

out = list(map(lambda i: "55" + i,"".join(line).split("55")[1:]))

这个 join 将所有数据合并在一起:

550009070156d24000f0059173060000ffffffff4400fd55000a0701ebd20048ffff01a8906d0000ffffffff38003c55000c070196d24000200200300580df100000ffffffff4a0052550009070156d2400240050b23480000ffffffff5000c155000c070196d24000000000000580badb0000ffffffff4700d9550009070156d2400130019ac0560000ffffffff4000f6550009070156d24000b00582e1760000ffffffff58002755000c070196d24000200300300580df270000ffffffff5200f8

这个 split 然后切片( [1:] )分离数据并删除前面的空元素:

['0009070156d24000f0059173060000ffffffff4400fd',
 '000a0701ebd20048ffff01a8906d0000ffffffff38003c',
 '000c070196d24000200200300580df100000ffffffff4a0052',
 '0009070156d2400240050b23480000ffffffff5000c1',
 '000c070196d24000000000000580badb0000ffffffff4700d9',
 '0009070156d2400130019ac0560000ffffffff4000f6',
 '0009070156d24000b00582e1760000ffffffff580027',
 '000c070196d24000200300300580df270000ffffffff5200f8']

以及 map 重新连接 55 到每一页的开头 list 呼叫只是将其从 map 对象返回到 list 用于打印)。

pgccezyw

pgccezyw4#

您可以定义一个接受 predicate 的生成器,如下所示:

def slice_at(predicate, iterable):
    i, x, size = 0, 0, len(iterable)
    while i < size-1:
        if predicate(iterable[i+1]):
            yield iterable[x:i+1]
            x = i + 1
        i += 1
    yield iterable[x:size]

然后您可以调用它来传递要应用的所需操作:


# lst = your list

tmp = slice_at(lambda x: x == '55', lst) # this return the sliced list (generator)
res = [ ''.join(x) for x in tmp ] # this joins the sublists

结果是:


# ['550009070156d24000f0059173060000ffffffff4400fd',

# '55000a0701ebd20048ffff01a8906d0000ffffffff38003c',

# '55000c070196d24000200200300580df100000ffffffff4a0052',

# '550009070156d2400240050b23480000ffffffff5000c1',

# '55000c070196d24000000000000580badb0000ffffffff4700d9',

# '550009070156d2400130019ac0560000ffffffff4000f6',

# '550009070156d24000b00582e1760000ffffffff580027',

# '55000c070196d24000200300300580df270000ffffffff5200f8']

相关问题