How can I update a list once an item has been removed from the list in the list in python

It's kind of tricky, but I would like to be able to update the larger list when an item has been taken out of the mini-list in the larger list.

listA = ['1','2','3','4','5','6','6','8','9','5','3','7']

      

I used the following code to break it down into lists of three

split = [listA[i:(i+3)] for i in range(0, len(listA) - 1, 3)]

print(split)

# [['1','2','3'],['4','5','6'],['6','8','9'],['5','3','7']]

split = [['1','2','3'],['4','5','6'],['6','8','9'],['5','3','7']]

      

if i removed #3

from the first list now the split will be

del split[0][-1]
split = [['1','2'],['4','5','6'],['6','8','9'],['5','3','7']]

      

after it #3

was removed, I would like to be able to update the list so that it looks like this:

split = [['1','2','4'],['5','6','6'],['8','9','5'],['3','7']]

early

+3


source to share


4 answers


Not sure how big this list gets, but you'll need to flatten it and recalculate it:



>>> listA = ['1','2','3','4','5','6','6','8','9','5','3','7']
>>> split = [listA[i:(i+3)] for i in range(0, len(listA) - 1, 3)]
>>> split
[['1', '2', '3'], ['4', '5', '6'], ['6', '8', '9'], ['5', '3', '7']]
>>> del split[0][-1]
>>> split
[['1', '2'], ['4', '5', '6'], ['6', '8', '9'], ['5', '3', '7']]
>>> listA = sum(split, []) # <- flatten split list back to 1 level
>>> listA
['1', '2', '4', '5', '6', '6', '8', '9', '5', '3', '7']
>>> split = [listA[i:(i+3)] for i in range(0, len(listA) - 1, 3)]
>>> split
[['1', '2', '4'], ['5', '6', '6'], ['8', '9', '5'], ['3', '7']]

      

+3


source


I need this to rotate cards in a deck in a solitaire game.

You can use your cards with a itertools.groupby()

nice key function:

def group_key(x, n=3, flag=[0], counter=itertools.count(0)):
    if next(counter) % n == 0:
        flag[0] = flag[0] ^ 1
    return flag[0]

      

^

is a bitwise operator , basically it changes the value of a flag from 0

to 1

and vice versa. The flag value is a list item because we are doing something like memoization.



Example:

>>> deck = ['1', '2', '3', '4', '5', '6', '6', '8', '9', '5', '3', '7']
>>> for k,g in itertools.groupby(deck, key=group_key):
...     print(list(g))
['1', '2', '3']
['4', '5', '6']
['6', '8', '9']
['5', '3', '7']

      

Now let's say you used cards "9" and "8", so your new deck looks like this:

>>> deck = ['1', '2', '3', '4', '5', '6', '6', '5', '3', '7']
>>> for k,g in itertools.groupby(deck, key=group_key):
...     print(list(g))
['1', '2', '3']
['4', '5', '6']
['6', '5', '3']
['7']

      

+2


source


Just re-create one list from your nested lists and then split again.

You can join lists, assuming they are only at the same level, with something like:

rejoined = [element for sublist in split for element in sublist]

      

There are no questionable ways, or one-liners, that use itertools or some other library, but don't overdo it. If you're only talking about a few hundred, or even a few thousand, this solution is good enough.

+2


source


Create an object that contains the list and tracks when the list is modified (perhaps by controlling write to it), then let the object split itself every time the data is changed, and store the list of sections to the member object.

0


source







All Articles