簡體   English   中英

將列表分成較小的列表,並遍歷每個較小的列表python

[英]separate list into smaller list and go through each smaller list python

我有這樣的清單

results = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0]

我想將此列表分開,以將項目分為4個元素:

size = 4
group_list_4 = [results[i:i+size] for i  in range(0, len(results), size)]
print "List:" , group_list_4

該命令的結果是這樣的:

List: [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], [1, 0, 0, 0]]

在每個4組中,我必須檢查1個元素在哪里,因此如果4組上的第一個元素為1,則第二個return "second"直到4,然后return "first" ,並將該值放入json_obj['value_1_in_list']

lista = []
for record in records:
            json_obj = {}
            json_obj['filename'] = filename
            json_obj['value_1_in_list'] =  put element 1 on list
            lista.append(json_obj)

在上面的代碼中,我有一個名為lista ,在其中創建JSON obj, for record in records:的條件for record in records:將執行17次,我還有17個包含4個元素的小列表。 對於每次執行for循環,都會創建一個JSON。 現在我想在此for loop內將值為1(第一,第二,第三,第四)的值包含在一個4元素的列表中,並且下一次將執行for循環以將其他小列表包含在包含4個元素的results list中我該怎么辦,有什么幫助嗎?

Lists with four elements always contain only one 1.

使用字典和列表的索引方法

碼:

test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1],
    [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1],
    [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1],
    [1, 0, 0, 0], [1, 0, 0, 0],[0,0,0,0]]
indexer={0:"first : ",1:"two : ",2:"three : ",3:"four : "}
for val in test:
    try:
        print indexer[val.index(1)],val
    except ValueError:
        print "No 1",val

輸出:

first :  [1, 0, 0, 0]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
three :  [0, 0, 1, 0]
first :  [1, 0, 0, 0]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
first :  [1, 0, 0, 0]
first :  [1, 0, 0, 0]
No 1 [0, 0, 0, 0]

Warning this may not work as expected for cases where there are more then one 1 in the list and OP has not told there will be situation like that

這可以以非常直接的方式實現。 您已經明確定義了標准(根據輸入返回值)。 那只是功能的規格。 編寫一個函數( one_location )並將其映射到您的數據集。

results = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0]

size = 4
group_list_4 = [results[i:i+size] for i  in range(0, len(results), size)]

def one_location(l):
    if l[0] == 1: return "first"
    elif l[1] == 1: return "second"
    elif l[2] == 1: return "third"
    elif l[3] == 1: return "fourth"

result_list = map(one_location, group_list_4)

list(result_list)

# ['first',
#  'first',
#  'fourth',
#  'first',
#  'fourth',
#  'third',
#  'first',
#  'first',
#  'fourth',
#  'fourth',
#  'fourth',
#  'first',
#  'fourth',
#  'fourth',
#  'fourth',
#  'first',
#  'first',
#  'first',
#  'first']
digit_word_map = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
chunks =  [results[i:i + 4] for i in range(len(results)) if i % 4 == 0]
for chunk in chunks:
    print digit_word_map[chunk.index(1) + 1]

使用itertools.compress :在子列表中工作超過1 s

>> q = ['1st','2nd', '3rd', '4th']
>> for i in range(0,len(results),4):
       f.append(compress(q,results[i:i+4]) or ["null"])

# [['1st', '4th'], ['1st'], ['4th'], ['1st'], ['4th'], ['3rd'], ['1st'], ['1st'], ['4th'], ['4th'], ['4th'], ['1st'], ['4th'], ['4th'], ['4th'], ['1st'], ['1st'], ['1st'], ['1st']]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM