繁体   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