如何创建一个没有重复的数字列表?

时间:2021-07-26 04:18:24

I tried using random.randint(0, 100), but some numbers were the same. Is there a method/module to create a list unique random numbers?

我试着使用随机的。randint(0,100),但有些数字是相同的。是否有方法/模块来创建列表唯一随机数?

def getScores():
    # open files to read and write
    f1 = open("page.txt", "r");
    p1 = open("pgRes.txt", "a");

    gScores = [];
    bScores = [];
    yScores = [];

    # run 50 tests of 40 random queries to implement "bootstrapping" method 
    for i in range(50):
        # get 40 random queries from the 50
        lines = random.sample(f1.readlines(), 40);

10 个解决方案

#1


86  

This will return a list of 10 numbers selected from the range 0 to 99, without duplicates.

这将返回从0到99之间选择的10个数字的列表,没有重复。

import random
random.sample(range(100), 10)

With reference to your specific code example, you probably want to read all the lines from the file once and then select random lines from the saved list in memory. For example:

对于您的特定代码示例,您可能想要从文件中读取所有的行,然后在内存中从保存的列表中选择随机的行。例如:

all_lines = f1.readlines()
for i in range(50):
    lines = random.sample(all_lines, 40)

This way, you only need to actually read from the file once, before your loop. It's much more efficient to do this than to seek back to the start of the file and call f1.readlines() again for each loop iteration.

这样,您只需要在循环之前从文件中实际读取一次。这样做比返回到文件的开始并为每次循环迭代再次调用f1.readlines()要高效得多。

#2


8  

You can first create a list of numbers from a to b, where a and b are respectively the smallest and greatest numbers in your list, then shuffle it with Fisher-Yates algorithm or using the Python's random.shuffle method.

您可以首先创建从a到b的数字列表,其中a和b分别是列表中最小和最大的数字,然后用Fisher-Yates算法或使用Python的random进行洗牌。洗牌的方法。

#3


7  

The solution presented in this answer works, but it could become problematic with memory if the sample size is small, but the population is huge (e.g. random.sample(insanelyLargeNumber, 10)).

这个答案中的解决方案是有效的,但是如果样本容量很小,那么内存就会有问题,但是总体是巨大的(例如随机的)。示例(insanelyLargeNumber,10))。

To fix that, I would go with this:

为了解决这个问题,我想说:

answer = set()
sampleSize = 10
answerSize = 0

while answerSize < sampleSize:
    r = random.randint(0,100)
    if r not in answer:
        answerSize += 1
        answer.add(r)

# answer now contains 10 unique, random integers from 0.. 100

#4


4  

You can use the shuffle function from the random module like this:

您可以使用随机模块中的shuffle函数,如下所示:

import random

my_list = list(xrange(1,100)) # list of integers from 1 to 99
                              # adjust this boundaries to fit your needs
random.shuffle(my_list)
print my_list # <- List of unique random numbers

Note here that the shuffle method doesn't return any list as one may expect, it only shuffle the list passed by reference.

这里要注意的是,shuffle方法并不像人们所期望的那样返回任何列表,它只会对引用传递的列表进行洗牌。

#5


3  

If the list of N numbers from 1 to N is randomly generated, then yes, there is a possibility that some numbers may be repeated.

如果从1到N的N个数的列表是随机生成的,那么是的,有可能有些数是重复的。

If you want a list of numbers from 1 to N in a random order, fill an array with integers from 1 to N, and then use a Fisher-Yates shuffle or Python's random.shuffle().

如果您想要一个从1到N的随机数列表,请在数组中填充从1到N的整数,然后使用Fisher-Yates shuffle或Python的random.shuffle()。

#6


2  

If you need to sample extremely large numbers, you cannot use range

如果你需要大量的样本,你就不能使用range。

random.sample(range(10000000000000000000000000000000), 10)

because it throws:

因为它将:

OverflowError: Python int too large to convert to C ssize_t

Also, if random.sample cannot produce the number of items you want due to the range being too small

同样,如果随机的。由于范围太小,示例无法生成所需的项目数量

 random.sample(range(2), 1000)

it throws:

它将:

 ValueError: Sample larger than population

This function resolves both problems:

该函数解决了这两个问题:

import random

def random_sample(count, start, stop, step=1):
    def gen_random():
        while True:
            yield random.randrange(start, stop, step)

    def gen_n_unique(source, n):
        seen = set()
        seenadd = seen.add
        for i in (i for i in source() if i not in seen and not seenadd(i)):
            yield i
            if len(seen) == n:
                break

    return [i for i in gen_n_unique(gen_random,
                                    min(count, int(abs(stop - start) / abs(step))))]

Usage with extremely large numbers:

大量使用:

print('\n'.join(map(str, random_sample(10, 2, 10000000000000000000000000000000))))

Sample result:

结果:样本

7822019936001013053229712669368
6289033704329783896566642145909
2473484300603494430244265004275
5842266362922067540967510912174
6775107889200427514968714189847
9674137095837778645652621150351
9969632214348349234653730196586
1397846105816635294077965449171
3911263633583030536971422042360
9864578596169364050929858013943

Usage where the range is smaller than the number of requested items:

使用范围小于被请求项目的数量:

print(', '.join(map(str, random_sample(100000, 0, 3))))

Sample result:

结果:样本

2, 0, 1

It also works with with negative ranges and steps:

它也适用于负的范围和步骤:

print(', '.join(map(str, random_sample(10, 10, -10, -2))))
print(', '.join(map(str, random_sample(10, 5, -5, -2))))

Sample results:

结果:样本

2, -8, 6, -2, -4, 0, 4, 10, -6, 8
-3, 1, 5, -1, 3

#7


0  

You can use Numpy library for quick answer as shown below -

您可以使用Numpy库进行快速回答,如下所示

Given code snippet lists down 6 unique numbers between the range of 0 to 5. You can adjust the parameters for your comfort.

给定的代码片段列出了0到5之间的6个唯一数字。你可以根据自己的舒适度调整参数。

import numpy as np
import random
a = np.linspace( 0, 5, 6 )
random.shuffle(a)
print(a)

Output

输出

[ 2.  1.  5.  3.  4.  0.]

It doesn't put any constraints as we see in random.sample as referred here.

它不像我们随机看到的那样,有任何约束条件。样本所称。

Hope this helps a bit.

希望这能有所帮助。

#8


-1  

If you wish to ensure that the numbers being added are unique, you could use a Set object

如果希望确保添加的数字是唯一的,可以使用Set对象

if using 2.7 or greater, or import the sets module if not.

如果使用2.7或更高,或导入set模块。

As others have mentioned, this means the numbers are not truly random.

正如其他人所提到的,这意味着数字并不是真正随机的。

#9


-1  

From the CLI in win xp:

从CLI win xp中:

python -c "import random; print(sorted(set([random.randint(6,49) for i in range(7)]))[:6])"

In Canada we have the 6/49 Lotto. I just wrap the above code in lotto.bat and run C:\home\lotto.bat or just C:\home\lotto.

在加拿大我们有6/49乐透。我只是把上面的代码打包成乐透。蝙蝠和运行C:\ \洛托回家。蝙蝠或C:\ \洛托回家。

Because random.randint often repeats a number, I use set with range(7) and then shorten it to a length of 6.

因为随机。randint经常重复一个数字,我使用set with range(7)然后将其缩短为6。

Occasionally if a number repeats more than 2 times the resulting list length will be less than 6.

有时,如果一个数字重复超过2倍,那么结果列表长度将小于6。

EDIT: However, random.sample(range(6,49),6) is the correct way to go.

编辑:然而,random.sample(range(6,49),6)是正确的方法。

#10


-2  

import random
result=[]
for i in range(1,50):
    rng=random.randint(1,20)
    result.append(rng)

#1


86  

This will return a list of 10 numbers selected from the range 0 to 99, without duplicates.

这将返回从0到99之间选择的10个数字的列表,没有重复。

import random
random.sample(range(100), 10)

With reference to your specific code example, you probably want to read all the lines from the file once and then select random lines from the saved list in memory. For example:

对于您的特定代码示例,您可能想要从文件中读取所有的行,然后在内存中从保存的列表中选择随机的行。例如:

all_lines = f1.readlines()
for i in range(50):
    lines = random.sample(all_lines, 40)

This way, you only need to actually read from the file once, before your loop. It's much more efficient to do this than to seek back to the start of the file and call f1.readlines() again for each loop iteration.

这样,您只需要在循环之前从文件中实际读取一次。这样做比返回到文件的开始并为每次循环迭代再次调用f1.readlines()要高效得多。

#2


8  

You can first create a list of numbers from a to b, where a and b are respectively the smallest and greatest numbers in your list, then shuffle it with Fisher-Yates algorithm or using the Python's random.shuffle method.

您可以首先创建从a到b的数字列表,其中a和b分别是列表中最小和最大的数字,然后用Fisher-Yates算法或使用Python的random进行洗牌。洗牌的方法。

#3


7  

The solution presented in this answer works, but it could become problematic with memory if the sample size is small, but the population is huge (e.g. random.sample(insanelyLargeNumber, 10)).

这个答案中的解决方案是有效的,但是如果样本容量很小,那么内存就会有问题,但是总体是巨大的(例如随机的)。示例(insanelyLargeNumber,10))。

To fix that, I would go with this:

为了解决这个问题,我想说:

answer = set()
sampleSize = 10
answerSize = 0

while answerSize < sampleSize:
    r = random.randint(0,100)
    if r not in answer:
        answerSize += 1
        answer.add(r)

# answer now contains 10 unique, random integers from 0.. 100

#4


4  

You can use the shuffle function from the random module like this:

您可以使用随机模块中的shuffle函数,如下所示:

import random

my_list = list(xrange(1,100)) # list of integers from 1 to 99
                              # adjust this boundaries to fit your needs
random.shuffle(my_list)
print my_list # <- List of unique random numbers

Note here that the shuffle method doesn't return any list as one may expect, it only shuffle the list passed by reference.

这里要注意的是,shuffle方法并不像人们所期望的那样返回任何列表,它只会对引用传递的列表进行洗牌。

#5


3  

If the list of N numbers from 1 to N is randomly generated, then yes, there is a possibility that some numbers may be repeated.

如果从1到N的N个数的列表是随机生成的,那么是的,有可能有些数是重复的。

If you want a list of numbers from 1 to N in a random order, fill an array with integers from 1 to N, and then use a Fisher-Yates shuffle or Python's random.shuffle().

如果您想要一个从1到N的随机数列表,请在数组中填充从1到N的整数,然后使用Fisher-Yates shuffle或Python的random.shuffle()。

#6


2  

If you need to sample extremely large numbers, you cannot use range

如果你需要大量的样本,你就不能使用range。

random.sample(range(10000000000000000000000000000000), 10)

because it throws:

因为它将:

OverflowError: Python int too large to convert to C ssize_t

Also, if random.sample cannot produce the number of items you want due to the range being too small

同样,如果随机的。由于范围太小,示例无法生成所需的项目数量

 random.sample(range(2), 1000)

it throws:

它将:

 ValueError: Sample larger than population

This function resolves both problems:

该函数解决了这两个问题:

import random

def random_sample(count, start, stop, step=1):
    def gen_random():
        while True:
            yield random.randrange(start, stop, step)

    def gen_n_unique(source, n):
        seen = set()
        seenadd = seen.add
        for i in (i for i in source() if i not in seen and not seenadd(i)):
            yield i
            if len(seen) == n:
                break

    return [i for i in gen_n_unique(gen_random,
                                    min(count, int(abs(stop - start) / abs(step))))]

Usage with extremely large numbers:

大量使用:

print('\n'.join(map(str, random_sample(10, 2, 10000000000000000000000000000000))))

Sample result:

结果:样本

7822019936001013053229712669368
6289033704329783896566642145909
2473484300603494430244265004275
5842266362922067540967510912174
6775107889200427514968714189847
9674137095837778645652621150351
9969632214348349234653730196586
1397846105816635294077965449171
3911263633583030536971422042360
9864578596169364050929858013943

Usage where the range is smaller than the number of requested items:

使用范围小于被请求项目的数量:

print(', '.join(map(str, random_sample(100000, 0, 3))))

Sample result:

结果:样本

2, 0, 1

It also works with with negative ranges and steps:

它也适用于负的范围和步骤:

print(', '.join(map(str, random_sample(10, 10, -10, -2))))
print(', '.join(map(str, random_sample(10, 5, -5, -2))))

Sample results:

结果:样本

2, -8, 6, -2, -4, 0, 4, 10, -6, 8
-3, 1, 5, -1, 3

#7


0  

You can use Numpy library for quick answer as shown below -

您可以使用Numpy库进行快速回答,如下所示

Given code snippet lists down 6 unique numbers between the range of 0 to 5. You can adjust the parameters for your comfort.

给定的代码片段列出了0到5之间的6个唯一数字。你可以根据自己的舒适度调整参数。

import numpy as np
import random
a = np.linspace( 0, 5, 6 )
random.shuffle(a)
print(a)

Output

输出

[ 2.  1.  5.  3.  4.  0.]

It doesn't put any constraints as we see in random.sample as referred here.

它不像我们随机看到的那样,有任何约束条件。样本所称。

Hope this helps a bit.

希望这能有所帮助。

#8


-1  

If you wish to ensure that the numbers being added are unique, you could use a Set object

如果希望确保添加的数字是唯一的,可以使用Set对象

if using 2.7 or greater, or import the sets module if not.

如果使用2.7或更高,或导入set模块。

As others have mentioned, this means the numbers are not truly random.

正如其他人所提到的,这意味着数字并不是真正随机的。

#9


-1  

From the CLI in win xp:

从CLI win xp中:

python -c "import random; print(sorted(set([random.randint(6,49) for i in range(7)]))[:6])"

In Canada we have the 6/49 Lotto. I just wrap the above code in lotto.bat and run C:\home\lotto.bat or just C:\home\lotto.

在加拿大我们有6/49乐透。我只是把上面的代码打包成乐透。蝙蝠和运行C:\ \洛托回家。蝙蝠或C:\ \洛托回家。

Because random.randint often repeats a number, I use set with range(7) and then shorten it to a length of 6.

因为随机。randint经常重复一个数字,我使用set with range(7)然后将其缩短为6。

Occasionally if a number repeats more than 2 times the resulting list length will be less than 6.

有时,如果一个数字重复超过2倍,那么结果列表长度将小于6。

EDIT: However, random.sample(range(6,49),6) is the correct way to go.

编辑:然而,random.sample(range(6,49),6)是正确的方法。

#10


-2  

import random
result=[]
for i in range(1,50):
    rng=random.randint(1,20)
    result.append(rng)