python 溢出错误:int太大,无法转换

rggaifut  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(141)

尝试生成随机短语,但不断出现错误:溢出错误:int太大,无法转换
尝试了numpy数组,但它没有解决这个问题,尝试了int32和64,但仍然得到同样的错误,代码与预设的单词数组一起工作,但当它们是随机生成的时,它不起作用,有什么想法可以增加int的大小吗?
谢谢

import itertools, hashlib

my_list = ['expect', 'push', 'live', 'math', 'truth', 'fashion', 'accident', 'uniform', 'midnight', 'provide', 'distance', 'lion', 'during', 'destroy', 'tennis', 'adult', 'deal', 'refuse', 'phone', 'latin', 'minute', 'mosquito', 'match', 'toilet', 'sell', 'wrap', 'tell', 'game', 'pistol', 'choice','fog', 'stairs', 'wing', 'theme', 'axis', 'turn', 'proud', 'face', 'awake', 'convince']

combs = itertools.permutations(my_list, 11)

counter = 0
file_counter = 1
entropy_possible = []
for item in combs:
    counter += 1
    line = ' '.join(item) + ' ?' + '\n'
    seed_phrase = line.split(" ")
    
    english = open("english.txt")
    word_list = english.read().split("\n")
    english.close()

    seed_phrase_index = [word_list.index(word) if word != "?" else word for word in item]
    seed_phrase_binary = [format(number, "011b") if number != "?" else number for number in seed_phrase_index]

    num_missing_bits = int(11-(1/3)*(len(item)))

    missing_bits_possible = [bin(x)[2:].rjust(num_missing_bits, "0") for x in range(2**num_missing_bits)]

    entropy_possible = ["".join(seed_phrase_binary[:-1])+bits for bits in missing_bits_possible]
    
    checksum = [format(hashlib.sha256(int(entropy, 2).to_bytes(len(entropy) // 8, byteorder="big")).digest()[0],"08b")[:11-num_missing_bits] for entropy in entropy_possible]
    
    last_word_bits = [i + j for i, j in zip(missing_bits_possible, checksum)]
    
     last_word = [word_list[int(bits, 2)] for bits in last_word_bits]

     print(last_word)
67up9zun

67up9zun1#

  • 你正在尝试使用int()函数将二进制字符串转换为整数,该函数导致error
  • 你可以尝试不同的方法来生成随机短语,而不是生成你的单词列表的所有可能的排列,你可以使用随机抽样技术从你的列表中选择一个子集的单词使用random.sample()
import random, hashlib

my_list = ['expect', 'push', 'live', 'math', 'truth', 'fashion', 'accident', 'uniform', 'midnight', 'provide', 'distance', 'lion', 'during', 'destroy', 'tennis', 'adult', 'deal', 'refuse', 'phone', 'latin', 'minute', 'mosquito', 'match', 'toilet', 'sell', 'wrap', 'tell', 'game', 'pistol', 'choice','fog', 'stairs', 'wing', 'theme', 'axis', 'turn', 'proud', 'face', 'awake', 'convince']

num_words = 11
num_samples = 100

for _ in range(num_samples):
    sample = random.sample(my_list, num_words)
    line = ' '.join(sample) + ' ?' + '\n'
    seed_phrase = line.split(" ")
    
    english = open("english.txt")
    word_list = english.read().split("\n")
    english.close()

    seed_phrase_index = [word_list.index(word) if word != "?" else word for word in sample]
    seed_phrase_binary = [format(number, "011b") if number != "?" else number for number in seed_phrase_index]

    num_missing_bits = int(11-(1/3)*(len(sample)))

    missing_bits_possible = [bin(x)[2:].rjust(num_missing_bits, "0") for x in range(2**num_missing_bits)]

    entropy_possible = ["".join(seed_phrase_binary[:-1])+bits for bits in missing_bits_possible]
    
    checksum = [format(hashlib.sha256(int(entropy, 2).to_bytes(len(entropy) // 8, byteorder="big")).digest()[0],"08b")[:11-num_missing_bits] for entropy in entropy_possible]
    
    last_word_bits = [i + j for i, j in zip(missing_bits_possible, checksum)]
    
    last_word = [word_list[int(bits, 2)] for bits in last_word_bits]

    print(last_word)
  • 另一种方法是使用random.choices()函数从列表中随机选择单词并直接生成短语,它避免了排列和二进制转换的需要。
import random

my_list = ['expect', 'push', 'live', 'math', 'truth', 'fashion', 'accident', 'uniform', 'midnight', 'provide', 'distance', 'lion', 'during', 'destroy', 'tennis', 'adult', 'deal', 'refuse', 'phone', 'latin', 'minute', 'mosquito', 'match', 'toilet', 'sell', 'wrap', 'tell', 'game', 'pistol', 'choice','fog', 'stairs', 'wing', 'theme', 'axis', 'turn', 'proud', 'face', 'awake', 'convince']

num_words = 11
num_samples = 100

for _ in range(num_samples):
    sample = random.choices(my_list, k=num_words)
    line = ' '.join(sample) + ' ?'
    print(line)

相关问题