简体   繁体   中英

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:

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

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. For each time that for loop will be executed a JSON is created. 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?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM