Ask Your Question
0

Inserting points in elements of a list

asked 3 years ago

anonymous user

Anonymous

updated 3 years ago

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?

Preview: (hide)

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 ( 3 years ago )

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

phcosta gravatar imagephcosta ( 3 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 3 years ago

updated 3 years ago

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
Preview: (hide)
link

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 ( 3 years ago )
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 ( 3 years ago )

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

phcosta gravatar imagephcosta ( 3 years ago )

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: 3 years ago

Seen: 255 times

Last updated: Aug 10 '21