I have a list of lists where I need to move the second and third element to be put in to the first element (which is a list) in first and last place respectively. I then need to remove the elements once used.
I already have a for loop which will work for the first instance, but I am unsure how to safely remove these elements and move to the next set and repeat operation. I understand for loops are not good for removing elements.
Example list
x = ['string 1', 1, 2, 'string 2', 0, 'string 3'], 'new string 1', 'new string 2', ['string 3', 19, 2, 'string 4', 0, 'string 5'], 'new string 3', 'new string 4', ['string 6', 1, 2, 'string 7', 0, 'string 8'], 'new string 5', 'new string 6', ['string 9', 19, 2, 'string 10', 0, 'string 11']
where new string 1
should be put in front of string 1
and new string 2
is appended to the list, then all first 10 values (technically the first 3 elements as the first is a list) are removed.
The following for loop achieves this for one instance
def sorting_insert_list(x):
x = list(x)
for i in range(len(x)):
keyword = x[1]
location = x[2]
x[0].append(location)
x[0].insert(0, keyword)
print(x)
and returns the following results
[['new string 1', 'string 1', 1, 2, 'string 2', 0, 'string 3', 'new string 2'], 'new string 1', 'new string 2', ['string 3', 19, 2, 'string 4', 0, 'string 5'], 'new string 3', 'new string 4', ['string 6', 1, 2, 'string 7', 0, 'string 8'], 'new string 5', 'new string 6']
The final list I require would look like the following:
sorted_list = ['new string 1', 'string 1', 1, 2, 'string 2', 0, 'string 3','new string 2',] ['new string 3''string 3', 19, 2, 'string 4', 0, 'string 5', 'new string 4',], ['new string 5','string 6', 1, 2, 'string 7', 0, 'string 8','new string 6']
It may be worth noting I will be doing this for a huge list, I have just simplified the data to show as an example. Thanks.
Adding attempt to incorporate date, which appears then in the list after new string 2
and in the position x[3] :
def sorting_insert_list(x):
result = []
for i in range(0, len(3)):
keyword = x[i + 1]
location = x[i + 2]
date = insert_values[i + 3]
result += [[keyword] + x[i] + [location] + date]
x = result
print(x)
I think the best way to do this is abandon original list x
, and create a new list to compose your result, instead of removing 2nd, 3nd elements from list, you can try this:
# list comprehension, more pythonic
def sorting_insert_list(x):
x = [[x[i+1]] + x[i] + [x[i+2]] for i in range(0, len(x) // 3 * 3, 3)]
print(x)
# base on your version
def sorting_insert_list(x):
result = []
for i in range(0, len(x) // 3 * 3, 3):
keyword = x[i + 1]
location = x[i + 2]
result += [[keyword] + x[i] + [location]]
x = result
print(x)
test:
x = [['string 1', 1, 2, 'string 2', 0, 'string 3'], 'new string 1', 'new string 2',
['string 3', 19, 2, 'string 4', 0, 'string 5'], 'new string 3', 'new string 4',
['string 6', 1, 2, 'string 7', 0, 'string 8'], 'new string 5', 'new string 6',
['string 9', 19, 2, 'string 10', 0, 'string 11']]
sorting_insert_list(x)
output:
[['new string 1', 'string 1', 1, 2, 'string 2', 0, 'string 3', 'new string 2'], ['new string 3', 'string 3', 19, 2, 'string 4', 0, 'string 5', 'new string 4'], ['new string 5', 'string 6', 1, 2, 'string 7', 0, 'string 8', 'new string 6']]
by the way, len(x) // 3 * 3
is used to ignore the tail which is less than 3, like your example. If you assure len(x) % 3 == 0
, you can just use len(x)
Hope that helps you, and comment if you have further questions. : )
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments