Changing value of variable in list using indices from another list

Trotom

I need to create a function that takes a sequence of integers, indices, with arbitrary length that will act as indices for a nested list, the_list and update the value at the final index with a new value, new_val. For example:

>>> the_list = [[1, 2], [3, 4], [5, 6]]
>>> indices = [2, 1]
>>> new_val = 'a'
>>> foo(the_list, indices, new_val)
[[1, 2], [3, 4],[5, 'a']]

This is equivalent to doing the_list[indices[0]][indices[1]] = new_val but I need to be able to this for a list of any length.

If list indeces has lenght 5 and the_list only 2 then the function uses only 2 first elements from indices.

omerbp

As mentioned in your edit, you Dont know indeces. In this case:

def get_ind(a,ind):
    if len(ind)==1:
        return a[0]
    return get_ind(a[ind[0]],ind[1:])

Some examples:

>>>a = [[["a", 15], [12, 0]], [[12, 4]]]
>>>indeces = [0,1,0]
>>>get_ind(a,indeces)
12
>>>a = [12, 4]
>>>indeces = [2]
>>>get_ind(a,indeces)
12

You can also use python exec in order to set/get the value:

>>>a = [[["a", 15], [12, 0]], [[12, 4]]]
>>>indeces = [0,1,0]
>>>newval = 217
>>>s = "a"+"".join(['['+str(x)+']'for x in indeces])+"="+str(newval )
>>>s
'a[0][1][0]=217'
>>>exec s
>>>a
[[["a", 15], [217, 0]], [[12, 4]]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a list of integers to reference the indices of another list to return a specific value from that list

From Dev

Remove elements from a list, using another list as indices

From Dev

Create a list with items from another list at indices specified in a third list

From Dev

Return values of a list for specific list of indices from another list

From Dev

Summing up elements from one list based on indices in another list

From Dev

Finding indices of items from a list in another list even if they repeat

From Dev

Variable is not changing value according to my list

From Dev

Changing Variable value from another class

From Dev

changing the value of a variable from another class in swift

From Dev

List of indices of every value

From Dev

List of indices of every value

From Dev

Grouping list indices by value

From Dev

Grouping column indices of an array based on max value of another list in python

From Dev

Grouping column indices of an array based on max value of another list in python

From Dev

Python: Changing the value of an instance from a list comprehension

From Dev

Changing value of droplist based on the value of another dropdown list

From Dev

Deleting elements from list using another list

From Dev

Sort a list using comparison from another list

From Dev

Deleting elements from list using another list

From Dev

Finding the indices of the values of one list in another list

From Dev

Remove parts of a list based on another list of indices

From Dev

Filter one list using index value obtained from another list using linq

From Dev

Changing values of the dataframe columns based on a list of indices

From Dev

Generate a list from list of dicts if value not exists in another list

From Dev

Creating a value list from dictionary with partial indices:values

From Dev

How to get a List of Indices from ElasticSearch using Jest

From Dev

How to get a List of Indices from ElasticSearch using Jest

From Dev

Javascript change variable after changing dropdown list value

From Dev

Substituting the values from list of lists by the values of another list based on indices in python

Related Related

  1. 1

    Using a list of integers to reference the indices of another list to return a specific value from that list

  2. 2

    Remove elements from a list, using another list as indices

  3. 3

    Create a list with items from another list at indices specified in a third list

  4. 4

    Return values of a list for specific list of indices from another list

  5. 5

    Summing up elements from one list based on indices in another list

  6. 6

    Finding indices of items from a list in another list even if they repeat

  7. 7

    Variable is not changing value according to my list

  8. 8

    Changing Variable value from another class

  9. 9

    changing the value of a variable from another class in swift

  10. 10

    List of indices of every value

  11. 11

    List of indices of every value

  12. 12

    Grouping list indices by value

  13. 13

    Grouping column indices of an array based on max value of another list in python

  14. 14

    Grouping column indices of an array based on max value of another list in python

  15. 15

    Python: Changing the value of an instance from a list comprehension

  16. 16

    Changing value of droplist based on the value of another dropdown list

  17. 17

    Deleting elements from list using another list

  18. 18

    Sort a list using comparison from another list

  19. 19

    Deleting elements from list using another list

  20. 20

    Finding the indices of the values of one list in another list

  21. 21

    Remove parts of a list based on another list of indices

  22. 22

    Filter one list using index value obtained from another list using linq

  23. 23

    Changing values of the dataframe columns based on a list of indices

  24. 24

    Generate a list from list of dicts if value not exists in another list

  25. 25

    Creating a value list from dictionary with partial indices:values

  26. 26

    How to get a List of Indices from ElasticSearch using Jest

  27. 27

    How to get a List of Indices from ElasticSearch using Jest

  28. 28

    Javascript change variable after changing dropdown list value

  29. 29

    Substituting the values from list of lists by the values of another list based on indices in python

HotTag

Archive