我正在使用 python 进行编码,并且有一个脚本可以生成 85 个字符的字符串的所有组合。有一个自定义映射将字符串中的字符映射到数字,因此我可以选择哪些字符串位于第一个或最后一个。我将 v 设置为映射中的最低字符,然后我想将 10 vs 添加到将来将生成的选定字符串,以便它排在第一位。只有一个字符串会添加 vs,因此它是唯一排在前面的字符串。
我的代码是:
import itertools
import string
hex_characters = '0123456789abcdef' # Exclude 'abcdef' since 'ABCDEF' is in 'string.hexdigits'
repeat = 85
product_generator = itertools.product(hex_characters, repeat=repeat)
def custom_mapping(hex_string):
# Define the custom mapping here
char_to_number = {
'0': 16,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 0,
'8': 8,
'9': 9,
'a': 10,
'b': 11,
'c': 12,
'd': 13,
'e': 14,
'f': 15,
'v': -1
}
# Sort characters according to the custom mapping
sorted_chars = sorted(hex_string, key=lambda char: char_to_number[char])
return ''.join(sorted_chars)
def onlyFunction2(combinations, exclude_char=''):
return (comb for comb in combinations if exclude_char not in comb)
filtered_combinations = onlyFunction2(product_generator, exclude_char='7')
for combination in filtered_combinations:
hex_string = ''.join(combination)
mapped_string = custom_mapping(hex_string)
print(mapped_string)
如您所见,在 onlyFunction2 中,有一个部分我可以排除特定字符,并且它可以跳过该字符。在排除某个字符的同时,我还想将 10 vs 添加到所选的 40 个字符长的子字符串中。我将自己编辑代码,以便每次迭代只有一个子字符串有效。如果你们可以帮助我将 10 vs 添加到选定的子字符串中,因为我的映射将 v 设置为 -1,该子字符串将排在第一位,并且只有一个字符串包含该子字符串(当我再次编辑代码时) .
举个例子,我想将
vvvvvvvvvv
添加到类似 的字符串中
3345000000000000000000000000000000000000000000000000000000000000000000000000000000000
在字符串的前面,所以它是:
vvvvvvvvvv3345000000000000000000000000000000000000000000000000000000000000000000000000000000000
我想仅使用 40 个字符的子字符串来完成此操作,因为我不知道最后 40 个字符左右。
我不确定我是否真的理解你想要什么,但这是我最好的尝试:
chosen_substring = '3345000000000000000000000000000000000000000000000000000000000000000000000000000000000'
for combination in filtered_combinations:
hex_string = ''.join(combination)
if hex_string.startswith(chosen_substring):
hex_string = 'v'*10 + hex_string
mapped_string = custom_mapping(hex_string)
print(mapped_string)