I have a list of tuples: I want to replace the second np.array of the first tuple with an array of the same length but filled with zeros: I have tried the following: But I get the: Any idea how to complete the replacement process ? Answer Tuples are immutable in Python, you cannot change values stored in it. You
Tag: tuples
Error when transforming (and reversing) Tuple Tuple to dict
I am trying to transform following tuple tuple into a dict (and a reversed one of that): This is the code I use: And I receive the following error: TypeError: unhashable type: ‘list’ How should I do this? Thank you a lot! Answer you are right, lists cannot be used as keys in python dict because key has to be
Inherit choice class to extend it?
I have a field in my models.py that accepts choices determined in a class: The choice class is this: My doubt is how can I inherit this UserChoices class to another choice class, in order to extend it with another options. I tried the following: But it gives me a migration error: Obviously this example is simplified, the actual code
Extract elements of a list of tuples based on elements in another list
I have a list of lists and a list of tuples I want to extract those tuples from list_tuples whose first element is listed in list_x. The desired output is I tried it by using itemgetter (from operator import itemgetter). It works well with dicts but I could not apply it to this problem since it cannot work with a
How to store multiple strings in list in Python?
I have this ListOfCoordinates list with 5 tuples (image below), with each index corresponding to the coordinates of two regions of the brain. I also have this variable named brain_areas_ROIs, which is a dictionary (image below) with the names of all possible brain regions. So I want to store in a dictionary the respective brain regions for all the pairs
iterate through a tuple to form a string
I am trying to create a function named make_string, that uses * correctly with a parameter: strings The function should return a string of all the values supplied joined, and separated by a space. Outside the make_string function, I declare a variable named my_string, and assign the value returned from the make_string function. Then I call the make_string function with
Sort a tuple with n elements based on a list with x elements
I have a tuple as given below I want to sort this tuple based on this list The sample of the expected output is given below I tried the following solution given at here to sort a tuple based on list. But it doesnt give the desired outcome. I tried using explicit loop but it doesnt work. Any help is
I need to sort two lists of tuples in different orders depending on whether elements in the tuples are equal or not
I have two possible lists, one is the name and the second is the age. my function needs to return copies of these lists such that they are both sorted in order of age (descending order) (ie the names of employees in the lst1 are also required to be ordered according to their ages in lst2). If two or more
Printing a list of tuples in a formated form with f-strings by a list comprehension
I want to print out a list of tuples in a formated form… I have the following list which contains a tuple formed by a string and other tuple of floats: I have the following code However it is causing a error such as I need to print them as follows: print the first element of the tuple, then for
Using Recursion to check for sum of tuples in a list
I have a func which takes a list of tuples, each tuple contains two items: item name and value. I need the func to return True if it’s possible to divide the tuple list into two equal valued groups and False otherwise. The function should be recursive and should not use any loops. for example, should result in True, because