frazman
frazman

Reputation: 33253

take corresponding values from first list and add to another list in python

I have two list of tuples as following:

actions = [(2, 1), (1, 0), 'lift 4', (2, 1), 'down 2', (1, 2), 'lift 4', 'down 2']
costs = [(2, 1, 3), (1, 0, 3), (2, 1, 3), (1, 2, 3)]

Here is the end result I want:

final_list = [(2, 1, 3), (1, 0, 3),'lift 4', (2, 1, 3),'down 2', (1, 2, 3),'lift 4','down 2' ]

What am I doing here.. The first two elements of tuple corresponds to (x,y) coordinates.. and then strings are "actions" i take in that coordinate. Now, in the "costs" list.. based on the motion (diagonal motion has cost 3 whereas linear motion has cost 2), I am appending cost to the tuple, ignoring the actions.

How do I merge these two lists so that i get the resulting "final_list"

Upvotes: 0

Views: 107

Answers (4)

mhawke
mhawke

Reputation: 87084

First create a dictionary to map the actions to costs:

actions = [(2, 1), (1, 0), 'lift 4', (2, 1), 'down 2', (1, 2), 'lift 4', 'down 2']
costs = [(2, 1, 3), (1, 0, 3), (2, 1, 3), (1, 2, 3)]
cost_map = {cost[:2] : cost for cost in costs}

Now you can use dict.get() with a default value to process the actions list:

final_list = [cost_map.get(action, action) for action in actions]
>>> final_list
[(2, 1, 3), (1, 0, 3), 'lift 4', (2, 1, 3), 'down 2', (1, 2, 3), 'lift 4', 'down 2']

Upvotes: 1

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8047

Rather straighforward approach:

costs.reverse()
final_list = []
for action in actions:
    if isinstance(action, tuple):
        # assume that costs in the order as actions
        final_list.append(costs.pop())
    else:
        final_list.append(action)
final_list

Upvotes: 1

Tom Zych
Tom Zych

Reputation: 13576

One solution is to simply treat costs as a lookup table. First, convert it to a handy dict:

cost_dict = {i[:2] : i for i in costs}

{(1, 2): (1, 2, 3), (1, 0): (1, 0, 3), (2, 1): (2, 1, 3)}

Then conditionally convert the values in actions:

final_list = [cost_dict[i] if isinstance(i, tuple) else i for i in actions]

[(2, 1, 3), (1, 0, 3), 'lift 4', (2, 1, 3), 'down 2', (1, 2, 3), 'lift 4', 'down 2']

Upvotes: 2

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48092

Assuming that the position of tuples actions and costs lists are mapped and are in order, in order to achieve the desired result you may create an intermediate list to store the index of tuple object in actions list as:

>>> actions = [(2, 1), (1, 0), 'lift 4', (2, 1), 'down 2', (1, 2), 'lift 4', 'down 2']
>>> costs = [(2, 1, 3), (1, 0, 3), (2, 1, 3), (1, 2, 3)]
>>> tuple_position = [i for i, a in enumerate(actions) if isinstance(a, tuple)]
>>> tuple_position
[0, 1, 3, 5]

Then update the values from costs list to the actions list based on the previous indexes as:

>>> final_list = list(actions)  # copy of `actions` list as you want the desired
                                # result in the form of new list
>>> for i, cost in zip(tuple_position, costs):
...     final_list[i] = cost
...
>>> final_list
[(2, 1, 3), (1, 0, 3), 'lift 4', (2, 1, 3), 'down 2', (1, 2, 3), 'lift 4', 'down 2']

Upvotes: 0

Related Questions