简体   繁体   English

将列表分成较小的列表,并遍历每个较小的列表python

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

I have a list like this 我有这样的清单

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]

I want to separate this list, to group items into 4elements together: 我想将此列表分开,以将项目分为4个元素:

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

the result of this command is this: 该命令的结果是这样的:

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

Into each 4-group I have to check where is 1 element so if the first element on a 4group is 1 it return "first" if second return "second" till four and that value to put inside json_obj['value_1_in_list'] . 在每个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)

In the code above I have a list called lista where I create JSON obj, the condition for record in records: will be executed 17 times, also I have 17 small lists with 4 elements. 在上面的代码中,我有一个名为lista ,在其中创建JSON obj, for record in records:的条件for record in records:将执行17次,我还有17个包含4个元素的小列表。 For each time that for loop will be executed a JSON is created. 对于每次执行for循环,都会创建一个JSON。 Now I want inside this for loop to include the value that is 1(first,second,third,fourth) inside one list of 4elements, and next time that for loop will be executed to include the other small list inside results list that contains 4elements how can i do that, any help? 现在我想在此for loop内将值为1(第一,第二,第三,第四)的值包含在一个4元素的列表中,并且下一次将执行for循环以将其他小列表包含在包含4个元素的results list中我该怎么办,有什么帮助吗?

Lists with four elements always contain only one 1.

Using a dictionary and list's index method 使用字典和列表的索引方法

Code: 码:

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

Output: 输出:

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

This can be implemented in a very straightforward way. 这可以以非常直接的方式实现。 You have clearly defined criteria (return a value based on an input). 您已经明确定义了标准(根据输入返回值)。 That is just a spec for a function. 那只是功能的规格。 Write a function ( one_location ) and map it to your data set. 编写一个函数( 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]

uses itertools.compress : works for more than one 1 s in a sublist 使用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