# Inserting points in elements of a list

I would like to transform a list, for example:

A = [((1, 1), (1, 1, 1, 1)),
((1, 1), (1, 1, 1, 1)),
((1, 1), (1, 1, 1, 2)),
((1, 1), (1, 1, 1, 3)),
((1, 1), (1, 1, 2, 1)),
((1, 1), (1, 1, 3, 1)),
((1, 1), (1, 2, 1, 1)),
((1, 1), (1, 2, 1, 2)),
((1, 1), (1, 2, 1, 3)),
((1, 1), (1, 3, 1, 1)),
((1, 1), (1, 3, 1, 2))]


into

A = [((1, 1, 0), (1, 1, 1, 1)),
((1, 1, 0), (1, 1, 1, 1)),
((1, 1, 0), (1, 1, 1, 2)),
((1, 1, 1), (1, 1, 1, 3)),
((1, 1, 1), (1, 1, 2, 1)),
((1, 1, 1), (1, 1, 3, 1)),
((1, 1, 2), (1, 2, 1, 1)),
((1, 1, 2), (1, 2, 1, 2)),
((1, 1, 2), (1, 2, 1, 3)),
((1, 1, 3), (1, 3, 1, 1)),
((1, 1, 3), (1, 3, 1, 2))]


Any suggestions?

edit retag close merge delete

Can you describe the algorithm used to convert from the old list to the new one? Is it just inserting zero at the end of the 0th tuple in your pair, and then increasing the inserted integer by 1 every three times?

( 2021-08-09 19:35:56 +0200 )edit

Yes, it is exactly that. I would like to do that in a list that has len(A) = 108.

( 2021-08-09 20:17:28 +0200 )edit

Sort by ยป oldest newest most voted

This function just inserts 0 at the end of the 0th tuple in each pair:

def change_list_wrong(A):
B = [] # new list
for pair in A:
B.append((pair[0] + (0,), pair[1]))
return B


So modify it to count the entries and increment every third time:

def change_list(A):
B = [] # new list
n = 0 # integer to insert
for i, pair in enumerate(A):
B.append((pair[0] + (n,), pair[1]))
# i counts the entries in A, so increment n every third time:
# whenever i is congruent to 2 mod 3
if i % 3 == 2:
n += 1
return B

more

Thank you. It is a very nice solution. Could you give me some references to learn a bit more about python/sagemath programming?

( 2021-08-10 03:54:32 +0200 )edit
1

What have you looked at? The Sage tutorial (https://doc.sagemath.org/html/en/tuto...) is a place to start, as is the Beginner's Guide to Python (https://wiki.python.org/moin/Beginner...).

( 2021-08-10 05:15:01 +0200 )edit

I have used that one: http://sagebook.gforge.inria.fr/

( 2021-08-12 14:50:24 +0200 )edit