I am still fairly new to Python. I have a list that includes numbers and lists of numbers:
list1 = [[3,3,2,2,1,1], 5, [2, 1], 2, [6,2,8]]
I have a function that will loop inside and determine if the item is a number or a list. If a list is detected, it will loop inside the inner list, otherwise continue.
def searchlist(ls):
global list1
for element in ls:
if isinstance(element,list):
searchlist(element)
elif (element == 2):
element += 3
list1[?][?] = element
The code obviously doesn't work but I'm looking for a way I can get the values of [?] for the actual indices of 2 in the list. So in the previous example, the following items will be replaced:
list1[0][2], list1[0][3], list1[2][0], list1[3], and list1[4][1]
I want this function to be able to change the value for the global variable (list1). So for example after calling the function searchlist on the list list1, the value of list1 should be:
list1 = [[3,3,5,5,1,1], 5, [5, 1], 5, [6,5,8]]
And I want to use this function on any list in the future, so the sizes aren't constant.
== 2, subtract 4 if it's==4, etc.—just add a couple moreelif`s. If you have a bunch of changes, try to come up with a way to represent them as a dict. See here on repl.it for some progressively more complicated but flexible things you could do.