Ask Your Question
0

Inserting points in elements of a list

asked 2021-08-09 17:13:00 +0200

anonymous user

Anonymous

updated 2021-08-09 17:19:52 +0200

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 flag offensive close merge delete

Comments

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?

John Palmieri gravatar imageJohn Palmieri ( 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.

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

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-08-10 00:05:23 +0200

updated 2021-08-10 01:56:58 +0200

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
edit flag offensive delete link more

Comments

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

phcosta gravatar imagephcosta ( 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...).

John Palmieri gravatar imageJohn Palmieri ( 2021-08-10 05:15:01 +0200 )edit

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-08-09 17:13:00 +0200

Seen: 67 times

Last updated: Aug 10 '21