Python 3.x 想要优化一个;电话号码生成器“;带循环演绎的代码

Python 3.x 想要优化一个;电话号码生成器“;带循环演绎的代码,python-3.x,performance,loops,for-loop,optimization,Python 3.x,Performance,Loops,For Loop,Optimization,程序说明: 1.根据你想要的电话号码,随机生成唯一的电话号码。我的意思是:如果你通过100,则生成100个电话号码 2.根据您传递给它的范围创建文本文件,我的意思是:如果您需要100个文本文件,其中包含100个唯一的电话号码,或者与其中的每个号码进行唯一比较,或者将另一个号码制作为文本文件 同时,它会创建电话号码,如果有意义的话,可以对电话号码进行如下排序: 文本文件中的预期格式: 1909911304 1987237347 。。。。。。。。。。。等等 这是负责执行此操作的方法: (注意

程序说明:

1.根据你想要的电话号码,随机生成唯一的电话号码。我的意思是:如果你通过100,则生成100个电话号码

2.根据您传递给它的范围创建文本文件,我的意思是:如果您需要100个文本文件,其中包含100个唯一的电话号码,或者与其中的每个号码进行唯一比较,或者将另一个号码制作为文本文件

同时,它会创建电话号码,如果有意义的话,可以对电话号码进行如下排序:

文本文件中的预期格式:

    • 1909911304
    • 1987237347
  • 。。。。。。。。。。。等等

    这是负责执行此操作的方法:

    注意:我使用make\u numbers方法作为操作的构造,实际上应该使用num\u doc\u amount。

    请注意:

    1.我唯一的问题是这些并行循环相互关联,我不知道是否可以简化代码。(请告诉我是否可以简化)。

    2.代码正常工作,没有错误。

        def make_numbers(self):
            """dont use this method:this method supports num_doc_amount method"""
            # sorry for this amount of loops it was inevitable to make the code work
            for number_of_files in range(self.amount_numbs):
                # this loop maintains the pi_digits.txt making(txt)
                number_of_files += 1
                if number_of_files == self.amount_files:
                    sys.exit()
                for phone_numbers in range(self.amount_numbs):
                    # This loop maintains the amount of phone numbers in each pi_digits.txt
                    file = open(f"{self.directory}\\{number_of_files}.{self.format}", 'w')
                    for numbers in range(self.amount_numbs):
                        # This loop is parallel to the previous one and
                        # writes that each number is which one from the
                        # whole amount of numbers
                        file.write(f"{numbers + 1}. - {self.first_fourz}{choice(nums)}"
                                   f"{choice(nums)}{choice(nums)}{choice(nums)}"
                                   f"{choice(nums)}{choice(nums)}{choice(nums)}\n")
    
        def num_doc_amount(self):
            """first make an instance and then you can use this method."""
            os.mkdir(f"{self.directory}")  # makes the folder
            for num_of_txt_files in range(self.amount_files):
                # This loop is for number of text files.
                num_of_txt_files += 1
                self.make_numbers()
    
    如果有任何方法可以简化此代码,请帮助我。谢谢。

        def make_numbers(self):
            """dont use this method:this method supports num_doc_amount method"""
            # sorry for this amount of loops it was inevitable to make the code work
            for number_of_files in range(self.amount_numbs):
                # this loop maintains the pi_digits.txt making(txt)
                number_of_files += 1
                if number_of_files == self.amount_files:
                    sys.exit()
                for phone_numbers in range(self.amount_numbs):
                    # This loop maintains the amount of phone numbers in each pi_digits.txt
                    file = open(f"{self.directory}\\{number_of_files}.{self.format}", 'w')
                    for numbers in range(self.amount_numbs):
                        # This loop is parallel to the previous one and
                        # writes that each number is which one from the
                        # whole amount of numbers
                        file.write(f"{numbers + 1}. - {self.first_fourz}{choice(nums)}"
                                   f"{choice(nums)}{choice(nums)}{choice(nums)}"
                                   f"{choice(nums)}{choice(nums)}{choice(nums)}\n")
    
        def num_doc_amount(self):
            """first make an instance and then you can use this method."""
            os.mkdir(f"{self.directory}")  # makes the folder
            for num_of_txt_files in range(self.amount_files):
                # This loop is for number of text files.
                num_of_txt_files += 1
                self.make_numbers()
    
    1.我唯一的问题是,这些并行循环相互关联,我不知道是否可以简化代码。(请告诉我是否可以简化。)

    即使这不是唯一的问题,在上面的代码中确实存在不必要的许多循环。它不需要超过两个:一个循环在文件上,一个循环在数字上;见下文

    2.代码运行正常,没有错误

    这是错误的,因为您希望所有电话号码都是唯一的。如上所述,《守则》没有规定书面电话号码是唯一的。要实现这一点,最简单的方法是一开始就生成所有唯一的数字

        def num_doc_amount(self):
            """first make an instance and then you can use this method."""
            os.mkdir(self.directory)    # makes the folder
            un = sample(range(10**7), self.amount_files*self.amount_numbs)  # all the unique numbers
            # This loop is for number of text files:
            for num_of_txt_file in range(self.amount_files):
                with open("%s/%s.%s"%(self.directory, 1+num_of_txt_file, self.format), 'w') as file:
                    # This loop maintains the amount of phone numbers in each .txt:
                    for number in range(self.amount_numbs):
                        # writes one number from the whole amount of numbers
                        file.write("%s. - %s%07d\n" %(1+number, self.first_fourz, un.pop()))
    
    

    什么是
    nums
    ?——虽然您说生成唯一的随机电话号码,但代码没有规定写入的电话号码是唯一的。抱歉@Armali,nums是一个变量,在类定义之前为其分配了一个包含0到9个单词的列表,我没有将其包含在代码中。如果您知道方法,请告诉我。
    nums=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    @Armali有没有办法省略这些循环中的一个,因为……你知道,每个循环都有一个特定的事情要做,第一个循环必须根据要生成手机号码的文件数进行循环,第二个循环是使用每个txt文件生成一种电话号码计数,第三个循环必须循环生成手机号码呃。这是正常的吗??!!还是物理的,或者是一个混乱的代码,包含很多不规则的地方。@Armali如何改进代码取决于您是否要求电话号码是唯一的,如果是的话,它们是否必须在一个文件中是唯一的,还是在所有文件中都是唯一的。您必须首先回答这个问题。@Armali,是的,我希望所有电话号码都是唯一的必须是唯一的,并且这种唯一性不应仅限于一个文件,为每个文件生成的所有电话号码必须是唯一的,并且必须与生成的电话号码存储在其中的其他txt文件相区别。