1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How do I turn 2 zipped itertools combinations variables into more combination that I...

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    Hello I'm currently coding a loop for combinations and I want to iterate through 2 zipped combinations in a new combinations of itertools.product. I already have the 2 zipped combinations in a variable in my iterator but when I put that zipped variable in my iterator and run the script then it doesn't print anything to console and ends.

    My code is:

    def custom_mapping(tuple_input):
    # Define the custom mapping here
    char_to_number = {
    '0': 0,
    '1': 1,
    '2': 2,
    '3': -3,
    '4': 4,
    '5': 5,
    '6': 6,
    '7': 7,
    '8': 8,
    '9': 9,
    'a': 10,
    'b': 11,
    'c': 12,
    'd': 13,
    'e': 14,
    'f': 15,
    'A': 16,
    'B': 17,
    'C': 18,
    'D': 19,
    'E': 20,
    'F': 21,
    'x': 22,
    }

    def sort_based_on_mapping(hex_string):
    # Sort the characters based on the custom mapping
    sorted_chars = sorted(hex_string,
    key=lambda char: char_to_number[char])
    return ''.join(sorted_chars)

    # Extract the items from the tuple
    item1 = tuple_input

    # Apply sorting to the first item
    sorted_item1 = sort_based_on_mapping(item1[:40])

    # Apply custom logic to the second item
    # Example: Simply sort it based on the same mapping or apply different logic
    #sorted_item2 = sort_based_on_mapping(item2)

    # Combine the sorted items
    return (sorted_item1 + item1[64:])



    def custom_sort(x):


    return x

    class MyIterator:
    def __init__(self,
    hex_string2,
    repeat2,
    hex_string,
    repeat,
    mapping_function2=None,
    mapping_function=None):
    # Initial strings and mappings
    self.hex_string = hex_string
    self.repeat = repeat
    self.hex_string2 = hex_string2
    self.repeat2 = repeat2
    self.mapping_function2 = mapping_function2
    self.mapping_function = mapping_function

    # Apply the first mapping to hex_string2, if provided
    if self.mapping_function2:
    self.hex_string2 = self.mapping_function2(self.hex_string2)

    # Create combinations of hex_string and the pre-mapped hex_string2
    self.combinations = itertools.product(self.hex_string, repeat=self.repeat)
    self.combinations2 = itertools.product(self.hex_string2, repeat=self.repeat2)

    # Zip combinations of hex_string and hex_string2
    self.linked_combinations = zip(self.combinations, self.combinations2)

    # Create the new combinations product with repeat=64
    self.current3 = itertools.product(self.linked_combinations, repeat=64)

    def __iter__(self):
    return self

    def __next__(self):
    # Get the next combined iteration from current3
    current = next(self.current3, None)

    # Stop if there are no more combinations
    if current is None:
    raise StopIteration

    # Flatten the combined tuple


    # Join all flattened combinations into a single string
    combined_string = ''.join(current)

    # Apply the second mapping to the combined string
    if self.mapping_function:
    combined_string = self.mapping_function(combined_string)

    # Return the final combined mapped output
    return combined_string

    def custom_mapping0(hex_string2):

    # Define the custom mapping here

    # Sort the filtered characters based on the custom mapping
    sorted_chars = sorted(hex_string2, key=custom_sort)

    # Return or print the sorted characters
    return ''.join(sorted_chars)


    # Example usage
    address = 1
    hex_string = '0123456789abcdef'
    repeat = 78
    hex_string2 = '0123456789abcdef'
    repeat2 = 33

    my_iterator = MyIterator(hex_string2,
    repeat2,
    hex_string,
    repeat,
    mapping_function2=custom_mapping0,
    mapping_function=custom_mapping)
    for string in my_iterator:

    account = string
    print(account)



    This is only the part that you guys would be interested in. When I run this code it doesn't print anything to console and it just ends in replit. I want to iterate through the combinations of the itertools.product one by one.

    Continue reading...

Compartilhe esta Página