Python 这个字符串到其他字符串的映射称为什么?

Python 这个字符串到其他字符串的映射称为什么?,python,string,hash,Python,String,Hash,我正在使用一个数据集,该数据集包含作为字符串的名称,需要公开发布,但原始名称不可见(即我需要能够区分不同的名称,但最终结果需要有类似于“e7fx8yuo”的内容,其中原始数据集有“John Doe”) 此方法的要求听起来与哈希过程类似,但要求较少(即我不需要可变长度名称映射到单个长度哈希),但名称需要映射到唯一字符串(两个不同的名称不能映射到同一字符串) 我计划用python写这篇文章,但我不完全确定我想要实现的过程到底是什么。如果可能的话,我还希望“散列”最终产品字符串的行为类似于github

我正在使用一个数据集,该数据集包含作为字符串的名称,需要公开发布,但原始名称不可见(即我需要能够区分不同的名称,但最终结果需要有类似于“e7fx8yuo”的内容,其中原始数据集有“John Doe”)

此方法的要求听起来与哈希过程类似,但要求较少(即我不需要可变长度名称映射到单个长度哈希),但名称需要映射到唯一字符串(两个不同的名称不能映射到同一字符串)


我计划用python写这篇文章,但我不完全确定我想要实现的过程到底是什么。如果可能的话,我还希望“散列”最终产品字符串的行为类似于github生成存储库名称建议的方式(“重新想象的内存”而不是“e7fx8yuo”,因为完整的单词字符串更容易记忆)。python中是否有任何模块可以为我做到这一点?

正如我在评论中所说,这听起来像是数据屏蔽。下面是一个基本实现:

from collections import defaultdict
from string import ascii_lowercase
from random import choice

random_strings = set()

def random_string():
    while True:
        result = ''.join(choice(ascii_lowercase) for _ in range(8))
        if result not in random_strings:
            random_strings.add(result)
            return result

masks = defaultdict(random_string)

print(masks['Adam'])
print(masks['Adam'])
print(masks['Bob'])
输出:

qmmwavuk
qmmwavuk
ykzlvfaf
{'H8WIAP': 'John Doe', '4NT7JC': 'Jane NoDoe', '208DBM': 'Getsum MoDoe'}
正如我在评论中所说,这听起来像是数据屏蔽。下面是一个基本实现:

from collections import defaultdict
from string import ascii_lowercase
from random import choice

random_strings = set()

def random_string():
    while True:
        result = ''.join(choice(ascii_lowercase) for _ in range(8))
        if result not in random_strings:
            random_strings.add(result)
            return result

masks = defaultdict(random_string)

print(masks['Adam'])
print(masks['Adam'])
print(masks['Bob'])
输出:

qmmwavuk
qmmwavuk
ykzlvfaf
{'H8WIAP': 'John Doe', '4NT7JC': 'Jane NoDoe', '208DBM': 'Getsum MoDoe'}
这里有一些又快又脏的事情要做

import string import random def id_generator(size=6, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) #with no arguments passed to this function it will return a 6 character string composed of letters and numbers def makeID(names): nameDict = {} for i in names: var = id_generator() while var in nameDict: #if the generator results already exist as a key we loop until we get a unique one var = id_generator() nameDict[var] = i #Here we set our key as the generator results, and set the value to the current name in the list which in this case is 'i' print(nameDict,) makeID(['John Doe','Jane NoDoe', 'Getsum MoDoe']) 导入字符串 随机输入 def id_生成器(大小=6,字符=string.ascii_大写+string.digits): 返回“”。join(random.choice(chars)for uuuin range(size))#如果未向此函数传递任何参数,它将返回由字母和数字组成的6个字符的字符串 def makeID(名称): nameDict={} 以我的名义: var=id\u生成器() 而nameDict中的var:#如果生成器结果已经作为键存在,我们将循环直到得到唯一的结果 var=id\u生成器() nameDict[var]=i#这里我们将键设置为生成器结果,并将值设置为列表中的当前名称,在本例中为“i” 打印(nameDict,) makeID(['John Doe'、'Jane NoDoe'、'Getsum MoDoe']) 输出:

qmmwavuk
qmmwavuk
ykzlvfaf
{'H8WIAP': 'John Doe', '4NT7JC': 'Jane NoDoe', '208DBM': 'Getsum MoDoe'} {'H8WIAP':'johndoe','4NT7JC':'janenodoe','208DBM':'getsummodoe'}
随机发生器来自这里有一些快速而肮脏的方法

import string import random def id_generator(size=6, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) #with no arguments passed to this function it will return a 6 character string composed of letters and numbers def makeID(names): nameDict = {} for i in names: var = id_generator() while var in nameDict: #if the generator results already exist as a key we loop until we get a unique one var = id_generator() nameDict[var] = i #Here we set our key as the generator results, and set the value to the current name in the list which in this case is 'i' print(nameDict,) makeID(['John Doe','Jane NoDoe', 'Getsum MoDoe']) 导入字符串 随机输入 def id_生成器(大小=6,字符=string.ascii_大写+string.digits): 返回“”。join(random.choice(chars)for uuuin range(size))#如果未向此函数传递任何参数,它将返回由字母和数字组成的6个字符的字符串 def makeID(名称): nameDict={} 以我的名义: var=id\u生成器() 而nameDict中的var:#如果生成器结果已经作为键存在,我们将循环直到得到唯一的结果 var=id\u生成器() nameDict[var]=i#这里我们将键设置为生成器结果,并将值设置为列表中的当前名称,在本例中为“i” 打印(nameDict,) makeID(['John Doe'、'Jane NoDoe'、'Getsum MoDoe']) 输出:

qmmwavuk
qmmwavuk
ykzlvfaf
{'H8WIAP': 'John Doe', '4NT7JC': 'Jane NoDoe', '208DBM': 'Getsum MoDoe'} {'H8WIAP':'johndoe','4NT7JC':'janenodoe','208DBM':'getsummodoe'}
随机生成器来自

,听起来对我来说效果很好,唯一的问题是如果名称与输出字符串的长度(8个字符)相比比较长,我有时会发生冲突。为了避免冲突,输出字符串的长度必须与字符串的最大长度相比较,是否有明确的规则?@BruceJohnJennerLawso我不知道你在说什么。代码设计为不允许冲突(至少在最终结果中不允许),并且代码的任何内容都与名称的长度无关。当应用于非常大的数据集时,会发生一些冲突。运行会产生冲突,例如:
u'Sherrie Andrew'->'hjoczvn'->u'hardcopyjamsovertimeconconjunctionszipvendorsnecks'
u'Jada Nevaeh'->'hjoczvn'->u'hardcopyjamsovertimeconconjunctionszipvendorsnecks'冲突。
@BruceJohnJennerLawso现在我忘了一行:
random\u字符串。添加(结果)
。这加强了独特性。如果键的长度为6,即26^6>3亿个唯一键,那么就有足够多的键可供使用。是的,速度要快得多。这对我来说非常有效,唯一的问题是,如果名称与输出字符串的长度(8个字符)相比相对较长,则有时会发生冲突。为了避免冲突,输出字符串的长度必须与字符串的最大长度相比较,是否有明确的规则?@BruceJohnJennerLawso我不知道你在说什么。代码设计为不允许冲突(至少在最终结果中不允许),并且代码的任何内容都与名称的长度无关。当应用于非常大的数据集时,会发生一些冲突。运行会产生冲突,例如:
u'Sherrie Andrew'->'hjoczvn'->u'hardcopyjamsovertimeconconjunctionszipvendorsnecks'
u'Jada Nevaeh'->'hjoczvn'->u'hardcopyjamsovertimeconconjunctionszipvendorsnecks'冲突。
@BruceJohnJennerLawso现在我忘了一行:
random\u字符串。添加(结果)
。这加强了独特性。如果键的长度为6,则26^6>3亿个唯一键,因此有足够多的键可供使用。是的,速度要快得多。