我使用 python 生成 74 个字符串的组合,并使用两个组合字符串,即“0123456789abcdef”和“QQQQQQ33”。我希望 QQQQQ33 位于括号 [] 中,以便在使用它时,它使用完整的字符串,而不是生成字符串与 0123456789abcdef 的所有组合。
每当我使用括号时,这就像是唯一的方法它完整地打印字符串而不是每个组合,它不会打印任何内容到控制台。它只是在 replit 中用复选标记完成代码,而不打印任何内容。
我使用括号的原因是因为我希望字符映射能够映射完整的字符串,以便当我将代码更改为其他字符串时类似于 AAAAAA9898 这样的 QQQQQQ33,它可以先选择这些字符并先映射它们。
到目前为止我的代码是:
import itertools
class MyIterator:
def __init__(self, full_iterable, hex_string, repeat, mapping_function=None):
self.full_iterable = full_iterable
self.hex_string = hex_string
self.repeat = repeat
self.mapping_function = mapping_function
if self.mapping_function:
self.full_iterable = self.mapping_function(self.full_iterable)
self.combinations = iter(self._generate_combinations())
self.current = next(self.combinations, None)
def _generate_combinations(self):
# Use full_iterable and hex_string in itertools.product
return itertools.product(self.full_iterable,self.hex_string, repeat=self.repeat)
def __iter__(self):
return self
def __next__(self):
if self.current is not None:
private_key = ''.join(self.current)
self.current = next(self.combinations, None)
return private_key
else:
raise StopIteration
def custom_mapping(hex_string):
# Define the custom mapping here
char_to_number = {
'0': 9,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 0,
'a': 10,
'b': 11,
'c': 12,
'd': 13,
'e': 14,
'f': 15,
'A': 16,
'B': 17,
'C': 18,
'D': 19,
'E': 20,
'F': 21,
'0xF977814e90dA44bFA03b6295A0616a897441aceC': -2,
}
filtered_chars = [char for char in hex_string if char in char_to_number]
# Sort the filtered characters based on the custom mapping
sorted_chars = sorted(filtered_chars,
key=lambda char: char_to_number[char])
return ''.join(sorted_chars)
full_iterable = ["QQQQQQ33"]
hex_string = '0123456789abcdef'
repeat = 74
my_iterator = MyIterator(full_iterable,
hex_string,
repeat,
mapping_function=custom_mapping)
for private_key in my_iterator:
account = private_key
print(account + "\n")
在 full_iterable 中,我使用括号将其设置为 [ ]。现在,当我取出括号时,它可以工作并打印组合,但不是我想要的组合类型。 The combinations it prints without brackets is,
30303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030
30303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303031
30303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303032
30303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303033
30303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034
30303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303035
I don't want it to generate the combinations like this.我希望它能生成与QQQQ...的组合,如下所示:
QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330 QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQ QQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ331QQQQQQ332QQQQQQ33d
QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQ QQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ 330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330 QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ331QQQQQQ332QQQQQQ33e
QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ33 0QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQ QQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ331QQQQQQ332QQQQQQ33f
请记住,字符映射还需要映射 QQQQQQ33 打印时是这样的。我将把 full_iterable 设置为许多与此类似的字符串,因此我需要使用映射来映射第一个和第二个完整字符串,依此类推。
该问题在于,正在对单个元素的可迭代对象(即
["QQQQQQ33"]
)使用
itertools.product
。这意味着产品中只会有一个元素,因此只会生成一个组合。
需要做的是为每个所需的输出位置创建单独的 "QQQQQQ33" 和十六进制字符的可迭代对象。然后,可以使用
itertools.product
从这些可迭代对象生成所有组合。
以下是对代码的修改,以生成所需的组合:
import itertools
class MyIterator:
def __init__(self, full_iterable, hex_string, repeat, mapping_function=None):
self.full_iterable = full_iterable
self.hex_string = hex_string
self.repeat = repeat
self.mapping_function = mapping_function
self.combinations = iter(self._generate_combinations())
self.current = next(self.combinations, None)
def _generate_combinations(self):
# 为每个重复创建 "QQQQQQ33" 和十六进制字符的可迭代对象
iterables = ([self.mapping_function(self.full_iterable)] if self.mapping_function else [self.full_iterable]) + ([self.hex_string] * self.repeat)
return itertools.product(*iterables)
def __iter__(self):
return self
def __next__(self):
if self.current is not None:
private_key = ''.join(self.current)
self.current = next(self.combinations, None)
return private_key
else:
raise StopIteration
def custom_mapping(hex_string):
# 定义自定义映射
char_to_number = {
'0': 9,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 0,
'a': 10,
'b': 11,
'c': 12,
'd': 13,
'e': 14,
'f': 15,
'A': 16,
'B': 17,
'C': 18,
'D': 19,
'E': 20,
'F': 21,
'0xF977814e90dA44bFA03b6295A0616a897441aceC': -2,
}
filtered_chars = [char for char in hex_string if char in char_to_number]
# 根据自定义映射对过滤后的字符进行排序
sorted_chars = sorted(filtered_chars,
key=lambda char: char_to_number[char])
return ''.join(sorted_chars)
full_iterable = ["QQQQQQ33"]
hex_string = '0123456789abcdef'
repeat = 74
my_iterator = MyIterator(full_iterable,
hex_string,
repeat,
mapping_function=custom_mapping)
for private_key in my_iterator:
account = private_key
print(account + "\n")
在此代码中:
-
我们修改了
_generate_combinations
函数,为每个输出位置创建单独的可迭代对象。 -
我们使用列表理解为每个重复创建
repeat
个hex_string
的副本。 -
我们将
full_iterable
(如果提供了mapping_function
,则为其映射版本)添加到可迭代对象列表的开头。 -
我们使用
*
运算符将可迭代对象列表解压缩为传递给itertools.product
的单独参数。
这将生成想要的组合,其中 "QQQQQQ33" 完整地包含在每个组合中,并且十六进制字符在每个 "QQQQQQ33" 之后变化。
标签:python,python-3.x,list,python-itertools From: 78841557