I'm having trouble thinking through this problem.
I have two lists as shown below.
my_orig_list = [(None, None), (None, None), (None, None), (None, None),(None, None), (None, None)]
new_values = [('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e')]
How can I get it to have (None, None)
as the final output as shown in the following desired output?
my_new_list = [('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e'), (None, None)]
I should also state that the length of new_values
will change from time to time.
I thought about determining the length of the list and then writing a function that would swap those values, but I cant seem to wrap my head around this relatively simple process.
If this has already been asked, I apologize. Maybe I don't know the terminology well enough to know what this would be called.
just slicing makes it easy ..
my_orig_list[:len(new_values)] = new_values[:]
keep in mind that new_values[:]
copies the list ie make another copy of it, whereas just doing.
my_orig_list[:len(new_values)] = new_values
will give a reference to it, both will work in this case as long as you don't manipulate the new_values
, how ever i would recommend new_values[:]
You can iterate through your new values, and use the index of the each array to update your original values like this:
for i in range(len(new_values)):
my_orig_values[i] = new_values[i]
This will set the first value of my_orig_values to the first value of new_values, and so on, until the end of new_values
可能在下面:
new_values.extend(my_orig_list[:-len(new_values)])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.