[Python] How do I use a custom mapping (sort) on my itertools for loop so that I can print...

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using python itertools specifically to generate combinations of integer strings. I have already created the loop that will loop through the combinations one at a time. I just need someone to help me input a custom mapping that will let me sort the combinations so that high priority strings will be printed first.

    My code so far is:

    from eth_keys import keys
    from sympy import invert
    from itertools import product, combinations
    import itertools

    def custom_mapping(combination):

    return combination

    def formula():

    def first_func(P1_c, P2_c, p, k):


    x1 = P1_c % k
    y1 = P1_c // k
    x2 = P2_c % k
    y2 = P2_c // k

    # Return combined number
    return x1 + k * y2

    def second_func(P_c, scalar, p, k,n):
    """Multiply a point represented as a combined number by a scalar using triple and add."""
    result10 = first_func(P_c, scalar, p, k)

    return result10


    k = 2 ** 140
    p = 9090
    Gx = 1010
    Gy = 2020
    G_c = Gx + k * Gy

    counter = itertools.count(start=1)

    for scalar_v in counter:
    combination = str(second_func(G_c, scalar_v, p, k, scalar_v)) # Calculate the public key


    for comb in itertools.combinations(combination + str(scalar_v), len(combination + str(scalar_v))):
    comb_string = ''.join(comb) # Join the characters to form a string

    # Print or do something with each combination
    print("Key:", comb_string)

    formula()


    In the custom_mapping function on top is where I want to use sorted() on my itertools combinations that will choose which combination I want and print it first. I'm not sure how this can be done and ChatGPT hasn't been much of a help.

    Can someone show me how this is done? I want to sort the combinations so that certain strings will be printed first.

    The type of sort I'm looking for is a character mapping like '0' : 1 and '1' : 0 so that certain strings with the characters 0 or 1 will come first or second.

    Continue reading...

Compartilhe esta Página