简体   繁体   中英

Change the element in a list of lists differently alternating every nth time

I have a list of lists containing information about days in a month.

eg

[[Day, Date, id, note]
[Day, Date, id, note]
[Day, Date, id, note]
[Day, Date, id, note]
[Day, Date, id, note]
[Day, Date, id, note]
[Day, Date, id, note]]

I would like to iterate through the list of lists setting the element 'id' to "A" for the first five then "B" for the second five and continuing that alternation for as long as the list of lists might be.

I am struggling to get the syntax right using a for loop.

Let's start with helper function. That function will take your list of list, first element you want to change, last one and character you want write into id field

def helper(list_of_list, start, end, char):
    for l in list_of_list[start:end]:
        l[2] = char

list[first:last] is a slicing syntax, it takes sublist [list[first], list[first+1]... list[last-1]] Now let's think how we want to call it? The calls should be:

helper(ll, 0, 5, "A")
helper(ll, 5, 10, "B")
helper(ll, 10, 15, "C")
...

So we can find how to write proper loop

c = "A"
for i in range(0, len(ll), 5):
    helper(ll, i, i+5, c)
    c = chr(ord(c) + 1)

The last parameter of range is astep , it will go through values 0, 5, 10, ... Last thing is how to increase the letter? We can convert it to ASCII code with ord() , add one and change it back to letter with chr() .

I didn't exactly understood, but from this and your other question that was closed, I suppose you mean that you want something like this:

from itertools import cycle, islice

letters = ['A','B']
chunk_size = 5

# we fill the list of letters until we meet the number of chunks
num_chunks = round(len(planner)/chunk_size)
letters_list = list(islice(cycle(letters), num_chunks))

# this let us split the list in groups of x size
def chunker(seq, size):
    return (seq[pos:pos + size] for pos in range(0, len(seq), size))

# we just traverse each group assigning the alternating letters defined above
for group in zip(letters_list, chunker(planner, chunk_size)):
    letter = group[0]
    for day in group[1]:
        day[2] = letter

If what you meant is to use a different letter for each chunk instead of only A and B then you can define letters as

import string
letters = string.ascii_uppercase

The zip function returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.

The cycle function from itertools like the word says, makes an iterator that let you travere the list like a circular linked list, the last element points again to the first element.

The islice function from itertools makes an iterator that traverses the iterable defining the start, stop, step values like range.

So mixing cycle and islice we can traverse the circular list until we meet our maximum which here is the number of chunks to pair letters with chunks.

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