我专门使用 python itertools 来生成整数字符串的组合。我已经创建了一次循环遍历组合的循环。我只需要有人帮助我输入自定义映射,让我对组合进行排序,以便首先打印高优先级字符串。
到目前为止我的代码是:
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()
在顶部的 custom_mapping 函数中,我想在 itertools 组合上使用排序(),它将选择我想要的组合并首先打印它。我不确定如何做到这一点,ChatGPT 也没有提供太大帮助。
有人可以告诉我这是如何完成的吗?我想对组合进行排序,以便首先打印某些字符串。
我正在寻找的排序类型是像 '0' : 1 和 '1' : 0 这样的字符映射,这样带有字符 0 或 1 的某些字符串将排在第一位或第二位。
希望我正确理解您的问题,如果没有,请告诉我,以便我尝试解决它。
基本上,您想要的是字符串的自定义比较器。
这是您的代码,但经过重写,以便您可以在
priority_mappings_dict
变量中自己指定每个数字的优先级。
from eth_keys import keys
from sympy import invert
from itertools import product, combinations
from functools import cmp_to_key
import itertools
import sys
priority_mappings_dict = {
"0": 1,
"1": 0
# .. so on
}
def comparator(comb1, comb2):
# Assuming len of both combs is always the same. If not, handle the edge cases
for i in range(len(comb1)):
# This will try to map each character to a integer from priority_dict above
# You should probably add representation for each digit, but just to be safe,
# if the character is not found in dict, this will return maximum possible value
# resulting in always being larger compared to anything else but itself
mapped_x = priority_mappings_dict.get(comb1[i], sys.maxsize)
mapped_y = priority_mappings_dict.get(comb2[i], sys.maxsize)
if mapped_x != mapped_y:
return mapped_x - mapped_y
return 0
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
# First of all, we convert the combination tuples to strings, as you did in your original for loop
stringified_combs = map(lambda x : ''.join(x), itertools.combinations(combination + str(scalar_v), len(combination + str(scalar_v))))
# Secondly, we sort these strings with our custom 'comparator' function defined above
sorted_combinations = sorted(stringified_combs, key=cmp_to_key(comparator))
for comb in sorted_combinations:
# Print or do something with each combination
print("Key:", comb)
formula()
为了解释更多一点,内置
sorted
以及我们的 comparator
函数,比较生成的组合中每对可能的值,返回一个
我们不仅比较第一个字符,而且我们正在比较在第一个字符处,我们的两个字符串不同之处(例如“1112”和“1113”,我们将比较“2”和“3”以及结尾) ,因为“111”前缀不会告诉我们任何有关优先级的信息)
另请注意,优先级字典中的映射以如下方式工作:右侧的数值越低,其优先级越高。
希望这有帮助。