Python 打印不重复的随机行

huangapple go评论62阅读模式
英文:

Python print random lines without repetitions

问题

我有一个小的Python程序,它会打印随机的十六进制字符串,每次运行程序时都会生成一个十六进制字符串。
我希望程序只生成一个不重复的十六进制字符串,这意味着程序不应生成已经生成过的字符串。

示例

python3 program.py

1. ab6b

2. 4c56

3. 1b13

4. ae8d

下一个示例显示输出是随机的,程序会重复行。

1b13

ae8d

4c56

ae8d

我的代码

import os
import binascii

print(binascii.hexlify(os.urandom(2)).decode('utf-8'))
英文:

I have a little python program which print random HEX string, it generates one HEX string each time I run the program.
i want the program to only generate a HEX string one's, that means the program should not generate a string that has already generated been generated.

EXAMPLE

python3 program.py

1. ab6b

2. 4c56

3. 1b13

4. ae8d

The next example show that the output is random and the program repeats lines.

1b13

ae8d

4c56

ae8d

My Code

import os
import binascii

print(binascii.hexlify(os.urandom(2)).decode('utf-8'))

答案1

得分: 5

我们可以存储所有先前生成的字符串,并检查任何新字符串是否与此列表相符,但在大量运行后,您可能会耗尽唯一的字符串。

import os
import binascii

已生成的十六进制集合 = set()

while True:
    新的十六进制 = binascii.hexlify(os.urandom(2)).decode('utf-8')
    if 新的十六进制 not in 已生成的十六进制集合:
        print(新的十六进制)
        已生成的十六进制集合.add(新的十六进制)
英文:

We could store all previously generated strings and check any new string against this list, but you might run out of unique strings after a large number of runs.

import os
import binascii

generated_hexes = set()

while True:
    new_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    if new_hex not in generated_hexes:
        print(new_hex)
        generated_hexes.add(new_hex)

答案2

得分: 1

为了这种情况,您需要将代码存储在某个地方,例如在Python中的一个列表中。

接下来,需要检查新代码是否存在于此列表中。

您可以根据需要调整以下代码。

import os
import binascii

list_codes = []

num = 1

while num == 1:
    hex_code = binascii.hexlify(os.urandom(2)).decode('utf-8')
    if hex_code not in list_codes:
        list_codes.append(hex_code)
        print(hex_code)
    num = int(input("Press 1 to continue "))

(Note: I've made a slight modification to the variable name 'hex' to 'hex_code' to avoid potential conflicts with the 'hex' built-in function in Python.)

英文:

For this situation, you need to store the codes in someplace, like a list in Python.

After this, is necessary to check if the new code exists in this list.

You can adapt the following code as you need.

import os
import binascii

list_codes = []

num = 1

while num == 1:
    hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    if hex not in list_codes:
      list_codes.append(hex)
      print(hex)
    num = int(input("Press 1 to continue "))

答案3

得分: 0

以下是翻译好的内容:

import os
import binascii

previously_printed = []  # 之前已打印的值
def print_new_random_hex():
    random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    while random_hex in previously_printed:  # 检查是否已经打印过
        random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    print(random_hex)
    previously_printed.append(random_hex)  # 将新值添加到已打印列表中

为了让计算机记住以前运行时打印的内容,你需要创建另一个文件来保存你所打印的值。在下面的代码中,文件名为 previously_printed.txt

import os
import binascii

with open('previously_printed.txt', 'r') as file:
    previously_printed = file.read().split('\n')  # 从文件中读取以前打印的值

with open('previously_printed.txt', 'a') as file:
    random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    while random_hex in previously_printed:  # 检查是否已经打印过
        print("yay we avoided printing", random_hex, "a second time!")
        random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')

    print(random_hex)
    file.write('\n' + random_hex)  # 将新值写入文件
英文:

You need to save the values you print and before printing the next value, check if it's been printed before.

import os
import binascii

previously_printed = []
def print_new_random_hex():
    random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    while random_hex in previously_printed:
        random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    print(random_hex)
    previously_printed.append(random_hex)

In order to have the computer remember what was printed from a previous run, you need to have another file going which saves the values you print. In the code below, the file is previously_printed.txt

import os
import binascii

with open('previously_printed.txt', 'r') as file:
    previously_printed = file.read().split('\n')

with open('previously_printed.txt', 'a') as file:
    random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')
    while random_hex in previously_printed:
        print("yay we avoided printing", random_hex, "a second time!")
        random_hex = binascii.hexlify(os.urandom(2)).decode('utf-8')

    print(random_hex)
    file.write('\n' + random_hex)

huangapple
  • 本文由 发表于 2023年6月13日 05:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定