【问题标题】:inserting all permutations of letters into sqlite [closed]将所有字母排列插入sqlite [关闭]
【发布时间】:2014-02-21 16:23:03
【问题描述】:

我想在我的字符列表中插入所有可能的 3 位数字排列及其哈希值到 Sqllite。任何人都可以帮助我修复这段代码:此外,我正在使用带有 2 个字段的 Cryptable 的 sqliteexpert:1)PlainText 2)Md5Hash,和sqlite3一样作为代码导入?

我试过了:

import hashlib
m = hashlib.md5()
import sqlite3
conn = sqlite3.connect('pr.db')
c = conn.cursor()
#print hashlib.md5("a").hexdigest()
mylist =[1,"A",3,"B",5,6,7,8,9,10]
print len(mylist)

for i in range(len(mylist)):
print hashlib.md5(str(mylist[i])).hexdigest()
c.execute('insert into Cryptable values (i)' , hashlib.md5(i))
connection.commit()
for j in range(len(mylist)):
c.execute('insert into Cryptable values (j)' , hashlib.md5(j))
connection.commit()
for k in range(len(mylist)):
c.execute('insert into Cryptable values (k)' , hashlib.md5(k))
connection.commit()

【问题讨论】:

    标签: python python-2.7 python-3.x sqlite sql-insert


    【解决方案1】:

    使用itertools 中的permutations 并调用hexdigest 以获取字符串形式的哈希:

    from hashlib import md5
    from itertools import permutations
    
    alphabet = "AB13567890"
    perm_length = 3
    perms = map("".join, permutations(alphabet, perm_length))
    hashes = [md5(k).hexdigest() for k in perms]
    
    for hash in hashes:
        # print("Do sql stuff: {}".format(hash))
    

    【讨论】:

    • 亲爱的 Butch,你能帮我编辑我自己的代码,而不是使用 itertools 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2018-03-10
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多