Search code examples
pythonlistlist-comprehension

How do I delete specific list items based on repeat values in Python?


I am pretty new to python and am trying to learn how to manipulate lists. This is stumping me and I am sure it is very easy to do without a hundred lines of code with if statements which is the only way I can think to do it.

Setup:

tileNUMS = ['4', '2', '4', '4', '2', '3', '4', '2', '2', '4', '1', '2', '2', '4', '3', '2', '2', '4', '3', '2', '4', '4', '3', '4', '4', '4', '4', '4']

TileList = [tileNUMS[i:i+4] for i in range (0, len(tileNUMS),4)]

Returns:

[['4', '2', '4', '4'], ['2', '3', '4', '2'], ['2', '4', '1', '2'], ['2', '4', '3', '2'], ['2', '4', '3', '2'], ['4', '4', '3', '4'], ['4', '4', '4', '4']]

The two outside numbers are values, and the two inside numbers are position.

What I would like is if the middle two values are repeated i.e. '4', '3' above, then only the last value of that repetition gets added to the list. i.e.

[['4', '2', '4', '4'], ['2', '3', '4', '2'], ['2', '4', '1', '2'], ['4', '4', '3', '4'], ['4', '4', '4', '4']]

After cleaning repeated positions, I want to end up with a list with every position (middle two numbers) from '1', '1' to '4', '4' and the value of that tile as the third item in the list, including 0 as a value if it is not present in the original list. i.e.

[['1', '1', 0], ['1', '2', 0], ['1', '3', 0], ['1', '4', 0], ['2', '1', 0], ... ['4', '4', '4']

I hope this makes sense. Thank you!!


Solution

  • Now that's a unique list manipulation.. Does every "tile" (4 elements list) always has first and last same elements?

    Let's assume it does so I will treat each tile as a 3 elements list. I took the liberty to change some more stuff, like using ints , not str to save your data.

    I'd recommend dictionaries.

    This should put you in a good start:

    tile_nums = ['4', '2', '4', '4', '2', '3', '4', '2', '2', '4', '1', '2', '2', '4', '3', '2', '2', '4', '3', '2', '4', '4', '3', '4', '4', '4', '4', '4']
    
    ROW_INDEX_POSITION = 1
    COL_INDEX_POSITION = 2
    VALUE_POSITION = 0
    
    BATCH_LEN = 4
    
    tile_list = [tile_nums[i: i + BATCH_LEN] for i in range (0, len(tile_nums), BATCH_LEN)]
    
    # dict would allow us to keep a UNIQUED collection which saves the latests values.
    only_latest_indexes = dict(((int(tile[ROW_INDEX_POSITION]), int(tile[COL_INDEX_POSITION])), int(tile[VALUE_POSITION])) for tile in tile_list)
    
    
    result = {}
    for i in range(1, BATCH_LEN + 1):
        for j in range(1, BATCH_LEN + 1):
            result[(i, j)] = only_latest_indexes.get((i, j), 0)
            
    print(result)