如何基于Python中的List中的特定值创建列表列表

时间:2017-08-08 16:42:13

标签: python python-3.x

我有一个这样的清单:

p = [
     [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 111, 102, 32], 
     [9, 9, 73, 116, 32, 105, 115, 32, 98, 101, 97, 117, 116, 105, 102, 117, 108, 108, 121, 46], 
     [78, 101, 101, 100, 32, 116, 111, 32, 102, 105, 103, 117, 114, 101, 32, 108, 105, 110, 101, 46], 
     [105, 116, 32, 97, 110, 100, 32, 108, 111, 118, 105, 110, 103, 32, 105, 116, 46, 46, 46], 
     [49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 46, 46, 46, 33, 33, 33]
    ]

我想创建一个值为32的每个值的列表列表。如果列表中没有值32,只需将所有内容放入新列表中。 我希望结果是这样的。

 p = [
      [[84, 104, 105, 115, 32], [105, 115, 32], [97, 32], [116, 101, 115, 116, 32], [111, 102, 32]], 
      [[9, 9, 73, 116, 32], [105, 115, 32], [98, 101, 97, 117, 116, 105, 102, 117, 108, 108, 121, 46]], 
      [[78, 101, 101, 100, 32], [116, 111, 32], [102, 105, 103, 117, 114, 101, 32], [108, 105, 110, 101, 46]], 
      [[105, 116, 32], [97, 110, 100, 32], [108, 111, 118, 105, 110, 103, 32], [105, 116, 46, 46, 46]], 
      [[49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 46, 46, 46, 33, 33, 33]]
    ]

是否可以这样做?如果是的话,你能帮我解决一下吗?

谢谢大家。

3 个答案:

答案 0 :(得分:1)

VALUE = 32

p = [
     [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 111, 102, 32], 
     [9, 9, 73, 116, 32, 105, 115, 32, 98, 101, 97, 117, 116, 105, 102, 117, 108, 108, 121, 46], 
     [78, 101, 101, 100, 32, 116, 111, 32, 102, 105, 103, 117, 114, 101, 32, 108, 105, 110, 101, 46], 
     [105, 116, 32, 97, 110, 100, 32, 108, 111, 118, 105, 110, 103, 32, 105, 116, 46, 46, 46], 
     [49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 46, 46, 46, 33, 33, 33]
    ]

result = []
for pi in p:
    sublists = []
    start = 0
    try:
        while start < len(pi):
            idx = pi.index(VALUE, start)
            sublists.append(pi[start:idx + 1])
            start = idx + 1
    except ValueError:
        if start < len(pi):
            sublists.append(pi[start:])
    result.append(sublists)

print(result)
>>> [[[84, 104, 105, 115, 32], [105, 115, 32], [97, 32], [116, 101, 115, 116, 32], [111, 102, 32]], [[9, 9, 73, 116, 32], [105, 115, 32], [98, 101, 97, 117, 116, 105, 102, 117, 108, 108, 121, 46]], [[78, 101, 101, 100, 32], [116, 111, 32], [102, 105, 103, 117, 114, 101, 32], [108, 105, 110, 101, 46]], [[105, 116, 32], [97, 110, 100, 32], [108, 111, 118, 105, 110, 103, 32], [105, 116, 46, 46, 46]], [[49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 46, 46, 46, 33, 33, 33]]]

答案 1 :(得分:1)

我觉得明确是这里最好的方法。

def split_on_value(iterable, value):
    result = []
    current = []
    for item in iterable:
        current.append(item)
        if item == value:
            result.append(current)
            current = []

    if current:
        result.append(current)

    return result

print [split_on_value(sub, 32) for sub in p]

答案 2 :(得分:1)

您可以使用itertools尝试:

from itertools import chain

p = list(chain.from_iterable(p))

places = [i+1 for i, a in enumerate(p) if a == 32]

places.insert(0, 0)
places.append(len(p))

new_data = [p[places[i]:places[i+1]] for i in range(len(places)-1)]

输出:

[[84, 104, 105, 115, 32], [105, 115, 32], [97, 32], [116, 101, 115, 116, 32], [111, 102, 32], [9, 9, 73, 116, 32], [105, 115, 32], [98, 101, 97, 117, 116, 105, 102, 117, 108, 108, 121, 46, 78, 101, 101, 100, 32], [116, 111, 32], [102, 105, 103, 117, 114, 101, 32], [108, 105, 110, 101, 46, 105, 116, 32], [97, 110, 100, 32], [108, 111, 118, 105, 110, 103, 32], [105, 116, 46, 46, 46, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 46, 46, 46, 33, 33, 33]]