Python:使用给定的数据集进程生成具有随机唯一编号的新列表

问题描述 投票:0回答:1

我有以下列表(作为 CSV 文件中的数据集)。有些列表包含连续的、不重复的整数,而另一些列表则包含不重复的整数。
如果我们观察,每个列表中的数字都有一定的级数(或模式),并且每个列表都绑定到 5 个数字/元素“N”:

sample-list-1.csv

N = 5

list1 = [1, 2, 3, 4, 5]       |  list9 = [25, 26, 27, 28, 29]   |  list17 = [3, 9, 15, 21, 27]
list2 = [2, 3, 4, 5, 6]       |  list10 = [26, 27, 28, 29, 30]  |  list18 = [9, 15, 21, 27, 33]
list3 = [7, 8, 9, 10, 11]     |  list11 = [31, 32, 33, 34, 35]  |  list19 = [4, 10, 16, 22, 28]
list4 = [8, 9, 10, 11, 12]    |  list12 = [32, 33, 34, 35, 36]  |  list20 = [10, 16, 22, 28, 34]
list5 = [13, 14, 15, 16, 17]  |  list13 = [1, 7, 13, 19, 25]    |  list21 = [5, 11, 17, 23, 29]
list6 = [14, 15, 16, 17, 18]  |  list14 = [7, 13, 19, 25, 31]   |  list22 = [11, 17, 23, 29, 35]
list7 = [19, 20, 21, 22, 23]  |  list15 = [2, 8, 14, 20, 26]    |  list23 = [6, 12, 18, 24, 30]
list8 = [20, 21, 22, 23, 24]  |  list16 = [8, 14, 20, 26, 32]   |  list24 = [12, 18, 24, 30, 36]

例如,list1中数字的级数/模式是

[number+1, etc.]
list16中数字的级数/模式是
[number+6, etc.]
,依此类推。

在 CSV 文件本身中,每个列表都位于单独的行中,列表中的每个数字位于单独的列中 - 没有标题或索引。仔细观察,列表中的数字可能代表一个矩阵,这是完全正确的,但是 N+1...即 6x6 矩阵:

6 x 6 matrix

1   2   3   4   5   6
7   8   9   10  11  12
13  14  15  16  17  18
19  20  21  22  23  24
25  26  27  28  29  30
31  32  33  34  35  36

为了简单起见,我们忽略矩阵,因为它与我们的案例无关。

记住给定的数据集列表:
我的目标只是生成一个新列表(或多个列表),其中包含 5 个非连续、非重复、唯一/随机数字,这些数字不遵循 sample-list-1 中所示的任何级数/模式.csv 数据集列表。
新生成的列表中的 5 个随机数应在 1 到 36 个数字的范围内(如矩阵所示)。
例如,新生成的列表应类似于以下内容:

list_new = [1, 6, 17, 23, 33] (or)
list_new = [8, 22, 24, 34, 36] (or)
list_new = [10, 11, 20, 32, 35] ...

...等等。
可以考虑一行中最多 2 个连续数字 (number+1) 或 2 个连续数字 (number+6) 的误差范围/容差,但生成的列表中的其他数字应该是完全随机的,并且也与给定的匹配限制(即没有进展)。
例如,带有误差范围/容差的新列表:

list_new = [1, 2, 17, 23, 33] (or)   -->  2 random generated elements match in list1
list_new = [2, 9, 15, 32, 36] (or)   -->  2 random generated elements match in list17/18
list_new = [5, 16, 22, 23, 34] ...   -->  2 random generated elements match in list19/20 & list7/8

这个想法是消除新生成的列表中形成进展/模式的任何可能性,因为我观察到,即使是随机数生成器代码有时仍然可以生成“progression”中的数字,如给定数据集中所示列表,例如:

not_desired = [2, 3, 4, 5, 6] (or)        -->  all random generated elements match in list2 and 4 in list1
not_desired = [8, 9, 10, 34, 36] (or)     -->  3 random generated elements match in list3/4
not_desired = [11, 17, 23, 29, 32] ...    -->  4 random generated elements match in list21/22

新生成的列表中元素的顺序并不重要。
目前,我可以一次从数据集中迭代一个列表并生成一个新列表...

但我主要希望通过在每次运行代码时一次性遍历数据集中的所有列表来生成一个新列表(每次运行都会生成一个更新的随机列表)...
这可以表示为类似于以下的粗略算法:

----- Run-1 -----
# iterate through lists 1 to 24 in one go #
-->
# generate a new list with random numbers that matches given constraints #

----- Run-2 -----
# iterate through lists 1 to 24 in one go #
-->
# generate a new list with random numbers that matches given constraints #

... and so on

我有以下代码片段,希望能帮助您实现目标:

import random
import csv

pick = random.sample(range(1, 37), 5)    # generate the new random number list
#print("pick:", pick)

with open('sample-list-1.csv', newline = '') as f:    # open the csv file to read the 24 lists
    reader = csv.reader(f)
    csvdata = list(reader)

csvdata_list = [int(x) for x in csvdata[2]]   # iterating one list at a time - which is time consuming!
#print("csvdata_list:", csvdata_list)

for n in pick:
    for i in csvdata_list:
        if n == i:
            print("n=", n, "i=", i)    # making a simple comparison

### How to iterate through ALL the dataset lists at once? ###

如果需要更多详细信息,请发表评论,我将更新问题。
预先感谢!

附注这个问题是我在这里问的问题的一个稍微简化的版本:
Python Networkx:生成具有给定数据集模式之外的随机唯一数字的列表

python list csv iteration
1个回答
0
投票

我最终设法解决了一个解决方法并回答了我自己的问题。
解决方法是检查新生成的列表是否存在于给定的数据集列表中。
如果不是,则逻辑继续。

© www.soinside.com 2019 - 2024. All rights reserved.