简体   繁体   English

如何在python中创建自定义数字列表

[英]how to create custom numbers list in python

I need to create txt files containing a custom list based on a specific pattren in python or any way faster any program or program language with for loop or any loop i cant doing that because i want to create 639936 files example to explaining txt files containing this我需要创建包含基于 python 中特定模式的自定义列表的 txt 文件,或者以任何方式更快的任何程序或程序语言使用 for 循环或任何我不能这样做的循环,因为我想创建 639936 个文件示例来解释包含此的 txt 文件

1000001_0001
1000001_0002
1000001_0003
1000001_0004
1000001_0005
1000001_0006
1000001_0007
1000001_0008
--
--
--
1000001_9996
1000001_9997
1000001_9998
1000001_9999

1000002_0001
1000002_0002
1000002_0003
1000002_0004
--
--
--
1000002_9999

another files change just loop from 1000001 to 9999999另一个文件改变只是循环从 1000001 到 9999999

9999999_0001
9999999_0002
9999999_0003
9999999_0004
--
--
--
9999999_9999

the first number start 1000001 end 9999999 the another number start 0001 end 9999 between that underscore _ list this 1000001_0001 i want to spilit files to create multiple files becuase too large第一个数字开始 1000001 结束 9999999 另一个数字开始 0001 结束 9999 在下划线之间_列出这个 1000001_0001 我想拆分文件以创建多个文件,因为太大

for i in range(1000001,9999999):
for y in range(0,9999):
    cv = "{:07d}".format(i)
    vc = format(y)
    print (cv+"_"+vc)
    
    with open('1000001.txt','ab') as f:
        f.write(cv+"_"+vc+"\n")

I am assuming that you are trying to write multiple files, each file containing a set of numbers with 2 parts separated by an underscore, and in each file you have a fixed first number and changing (incrementing) "second number" from 0000 to 9999, and different files have different "first numbers" from 1000001 to 9999999.我假设您正在尝试编写多个文件,每个文件都包含一组数字,其中 2 个部分由下划线分隔,并且在每个文件中,您都有一个固定的第一个数字,并将“第二个数字”从 0000 更改(递增)到 9999 , 不同的文件有不同的“第一个数字”,从 1000001 到 9999999。

If you are doing this in Python, List Comprehension is your friend.如果您在 Python 中执行此操作,List Comprehension 是您的朋友。 With list comprehension, you can create a list of items with any pattern, with a single command.使用列表理解,您可以使用单个命令创建具有任何模式的项目列表。

custom_list = ['1000001_' + str('%04d') % x for x in range(10000)]

# output: list of numbers as strings

Code explanation:- Variable x is looped from 0 to 9999 (10000-1).代码说明:- 变量 x 从 0 到 9999 (10000-1) 循环。 In each iteration, the x value is passed into function str() to convert integer into string format (you can't have underscore in a number, hence converting to string).在每次迭代中,x 值传递给函数str()以将整数转换为字符串格式(数字中不能有下划线,因此转换为字符串)。 '%04d' makes sure each x value is of 4 digits, by adding zeroes in front when needed. '%04d'通过在需要时在前面添加零来确保每个 x 值都是 4 位数字。 Each such strings made ('0000', '0001', ...) is then concatenated to the first number as string.每个这样的字符串 ('0000', '0001', ...) 然后连接到第一个数字作为字符串。

You can run another loop to loop through the first numbers and put this above written loop inside that, and make the outer loop save each list into a new file in each iteration (like converting into numpy array and using numpy.save).您可以运行另一个循环来遍历第一个数字并将上面编写的循环放入其中,并使外循环在每次迭代中将每个列表保存到一个新文件中(例如转换为 numpy 数组并使用 numpy.save)。 If you don't want to save these numbers into different files, you can even convert the above function into a nested list comprehension and make a 2D list.如果你不想将这些数字保存到不同的文件中,你甚至可以将上面的函数转换为嵌套列表理解并制作一个二维列表。

Cheerio!啦啦啦!

Edit: (adding code after OP's comment)编辑:(在 OP 的评论后添加代码)

If you don't have numpy installed before you run this code, make sure you do.如果您在运行此代码之前没有安装 numpy,请确保您安装了。

# importing functions from numpy
from numpy import save

number_list = []
number_of_files = 1
for (index, i) in enumerate(range(1000001, 10000000)):
  small_list = [str(i) + '_' + str('%04d') % x for x in range(10000)]
  number_list += small_list

  # "save-test":
  # checking if number_list has enough elements for 1 file,
  # then saves it and empties the variable
  if (index + 1) % 9001 == 0:
    save('number_list' + str((index + 1) // 9001), number_list)
    number_list = []
    number_of_files += 1

# adding last file contents manually if it doesn't clear above "save-test"
if len(number_list) > 0:
  save('number_list' + str(number_of_files), number_list)

This will generate 1000 files, with 9001 * 10000 values in all files except last, which contains lesser number of values.这将生成 1000 个文件,除最后一个文件外,所有文件中都有 9001 * 10000 个值,其中包含较少数量的值。

I don't know what you are trying to do with this code, but this should give you the results you mentioned in the comments.我不知道你想用这段代码做什么,但这应该会给你你在评论中提到的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM