我正在使用 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 中,我使用括号将其设置为 [ ]。现在,当我取出括号时,它可以工作并打印组合,但不是我想要的组合类型。它打印的不带括号的组合是,
3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030 3030303030
3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030 3030303031
3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030 3030303032
我不希望它生成这样的组合。我希望它能生成与QQQQ的组合...在那里像这样:
QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQ QQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ331QQQQQQ332QQQQQQ33d
QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQ QQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ331QQQQQQ332QQQQQQ33e
QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQ QQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ330QQQQQQ3 30QQQQQQ331QQQQQQ332QQQQQQ33f
请记住,在像这样打印时,字符映射还需要映射QQQQQQ33。我将把 full_iterable 设置为许多与此类似的字符串,因此我需要使用映射来映射第一个和第二个完整字符串,依此类推。
出现您遇到的问题是因为 itertools.product 会根据单个字符生成组合,除非您明确告诉它将某些字符串视为单个单元,并确保“QQQQQQ33”被视为单个实体而不是拆分成多个单个字符,您应该将其作为列表的元素传递。
以下是修改代码的方法:
当您将 [“QQQQQQ33”] 传递给 itertools.product 时,它将“QQQQQQ33”视为一项,生成“QQQQQQ33”保持不变的组合。
您可以使用自定义迭代器来生成组合,确保正确使用您的特殊字符串。
以下是实现方法:
import itertools
class MyIterator:
def __init__(self, full_iterable, repeat):
self.full_iterable = full_iterable
self.repeat = repeat
self.combinations = iter(itertools.product(self.full_iterable, repeat=self.repeat))
self.current = next(self.combinations, None)
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
full_iterable = ["QQQQQQ33"]
# Adjust this according to how many times you want to repeat the combination
repeat = 1
my_iterator = MyIterator(full_iterable, repeat)
for private_key in my_iterator:
print(private_key)