首页 > 编程问答 >如何修复当我将组合放入这样的列表 ([ 地址 )] 时,它说它是空的并且不打印任何内容的错误?

如何修复当我将组合放入这样的列表 ([ 地址 )] 时,它说它是空的并且不打印任何内容的错误?

时间:2024-08-08 03:48:52浏览次数:11  
标签:python python-3.x list python-itertools

我使用 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

相关文章

  • 如何在Python中绘制伪球面
    目标是使用meshgrid和numpy库生成伪球体的三维图形,但我使用下面的代码生成的图形不完整u=np.linspace(0,np.pi,50)v=np.linspace(0,2*np.pi,100)x,y=np.meshgrid(u,v)X=np.arccos(x)*np.cos(y)Y=np.arccos(x)*np.sin(y)Z=x-np.tan(x)fig=plt.f......
  • 18:Python集合属性
    #Python3集合#集合(set)是一个无序的不重复元素序列。#集合中的元素不会重复,并且可以进行交集、并集、差集等常见的集合操作。#集合中元素必须是不可变类型,也就说里面不能是列表和字典#可以使用大括号{}创建集合,元素之间用逗号,分隔,或者也可以使用set()函数创建集合。s......
  • Python爬虫案例与实战:爬取源代码练习评测结果
    Python爬虫案例与实战:爬取源代码练习评测结果本章案例将介绍用Python编写程序实现简单网站的模拟登录,然后保持登录后的网页会话,并在会话中模拟网页表单提交,之后使用Requests库的高级特性爬取提交之后的返回结果。在HTTP网页中,如登录、提交和上传等操作一般通过向网页发送......
  • Python爬虫案例与实战:爬取豆瓣电影简介
    Python爬虫案例与实战:爬取豆瓣电影简介本章案例将介绍如何爬取豆瓣电影简介,以此帮助读者学习如何通过编写爬虫程序来批量地从互联网中获取信息。本案例中将借助两个第三方库----Requests库和BeautifulSoup库。通过Requests库获取相关的网页信息,通过BeautifulSoup库解析大......
  • 趣味Python游戏编程:第3章 递归函数的威力:扫雷
    趣味Python游戏编程:第3章递归函数的威力:扫雷在第2章中,我们制作了一个拼图游戏,玩家通过鼠标操作图片块移动。本章设计一款扫雷游戏,玩法是在一个方块阵列中随机埋设一定数量的地雷,然后由玩家逐个打开方块,并以排除所有地雷为最终游戏目标。如果玩家打开的方块中有地雷,则游戏......
  • Python 依赖管理神器 Poetry 深入指南
    Poetry依赖管理详细教程引言在现代软件开发中,依赖管理是项目成功的关键。Python开发者现在有了一个新的选择:Poetry。Poetry是一个现代的Python包管理工具,旨在简化Python项目的创建、打包和发布。它提供了一种更直观和高效的方式来管理项目的依赖关系,相较于传统的p......
  • python的函数、魔法方法和案例
    1.python函数中self的用法在Python中,self 是一个对当前实例(对象)的引用,用于访问属于该类的变量和方法。在定义类的方法时,通常需要将 self 作为第一个参数。这允许在类的实例上调用方法时,自动将实例本身作为参数传递给方法。classPerson:def__init__(self,name......
  • 【数据结构】LinkedList与链表
    目录链表1、链表的概念及结构 2、LinkedList的使用2、1什么是LinkedList2、2LinkedList的使用3、LinkedList的遍历4、LinkedList的模拟实现 5、ArrayList和LinkedList的区别上篇已经熟悉了ArrayList的使用,ArrayList底层使用数组来存储元素。由于其底层是一段连续......
  • 【视频讲解】Python灰色关联度分析直播带货效用、神经退行性疾病数据
    原文链接:https://tecdat.cn/?p=37273原文出处:拓端数据部落公众号 分析师:JiayiDeng 本文展示如何用灰色关联度分析的直播带货效用及作用机制研究,并结合一个分析神经退行性疾病数据实例的代码数据,为读者提供一套完整的实践数据分析流程。一、研究难点直播带货作为新兴产业,缺......
  • 线性方程组迭代算法的Python实现
    更多精彩,关注博客园主页,不断学习!不断进步!我的主页csdn很少看私信,有事请b站私信博客园主页-发文字笔记-常用有限元鹰的主页内容:ABAQUS数值模拟相关Python科学计算开源框架,编程学习笔记哔哩哔哩主页-发视频-常用FE-有限元鹰的个人空间内容:模拟案例网格划分游戏视频,......