2017-08-08 51 views
-6

我有這樣一個名單列表:如何創建列表的基礎上一定值的列表在Python

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]] 
    ] 

是否可以這樣做?如果是這樣,請你給我一個解決方案?

謝謝大家。

+4

「做功課的我」 –

+1

等都不是代碼編寫的服務,請嘗試自己的代碼然後提出嘗試和問題,我們可以幫助 –

+1

FWIW,'b''.join(map(bytes,p))'returns'b'This is a test of \ t \ tIt是美麗的。需要圖線。它和愛它... 1234567890 ... !!!' –

回答

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]]] 
+0

這是一個美麗的解決方案。我完全忘記了列表附加列表。不知道如何創建其他列表自動阻止了我的想法。非常感謝。 – ryanvolap

+0

可能值得注意的是,這種方法是O(n^2),而只需要O(n)。 –

+0

@JaredGoguen我不認爲是這樣。我已經做了一些改變,以避免一些列表副本應該更清楚。每個'index'操作從前一個完成的地方開始,因此總共它們遍歷每個子列表一次。 – jdehesa

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] 
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]] 
相關問題