简体   繁体   English

如何从 2 个字符串生成唯一键?

[英]How can I generate a unique key from 2 strings?

Let's say I have "username1" and "username2"假设我有“username1”和“username2”

How can I combine these two usernames together to generate a unique value?如何将这两个用户名组合在一起以生成唯一值?

The value should be the same no matter how they are combined if username2 is entered first or vice versa the two names should always combine to become the same unique value.如果首先输入 username2,则无论它们如何组合,该值都应该相同,反之亦然,这两个名称应该始终组合成相同的唯一值。 The string length will not work as other usernames can have the same length.字符串长度不起作用,因为其他用户名可以具有相同的长度。

Is there a simple way to do this or a technique for this?有没有一种简单的方法来做到这一点或一种技术?

Sets are unordered, and Python has a hashable immutable set type, assuming your requirement for a unique key is that it can be used as a dict key:集合是无序的,并且 Python 具有可散列的不可变集合类型,假设您对唯一键的要求是它可以用作字典键:

def key(a, b):
    return frozenset([a, b])

d = {}
d[key("foo", "bar")] = "baz"
print(d[key("bar", "foo")])

You can also create a sorted tuple:您还可以创建一个排序的元组:

def key(a, b):
    return tuple(sorted([a, b]))

d = {}
d[key("foo", "bar")] = "baz"
print(d[key("bar", "foo")])

There are many ways to generate a unique value.有很多方法可以产生独特的价值。 One common (to sysops) method is the one used to get a UNIX UUID (Universally Unique IDentifier) from the routines supplied with most major languages.一种常见的(系统操作员)方法是用于从大多数主要语言提供的例程中获取 UNIX UUID(通用唯一标识符)的方法。 In case of parsing problems, use a separator that does not appear in your input strings.如果出现解析问题,请使用未出现在输入字符串中的分隔符。

If you replace my constant strings "username1" and "username2" with your variables, I believe that your problem is solved.如果你用你的变量替换我的常量字符串“username1”和“username2”,我相信你的问题已经解决了。

import uuid
unique_sep = '|'    # I posit vertical bar as not appearing in any user name

import uuid
unique_ID = uuid.uuid5(uuid.NAMESPACE_X500, "username1" + unique_sep + "username2")
print(unique_ID)

Output: Output:

b2106742-94f5-596d-a461-c977b5982d85

To preserve uniqueness from entry order, simply sort them in any convenient fashion, such as:为了保持输入顺序的唯一性,只需以任何方便的方式对它们进行排序,例如:

names_in = [username1, username2]
unique_ID = uuid.uuid5(uuid.NAMESPACE_X500, min(names_in) + unique_sep + max(names_in))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 dataframe 中的唯一字符串生成唯一的 int - how can I generate a unique int from a unique string in dataframe 如何从字符串的 DataFrame 列中获取唯一单词? - How can I get unique words from a DataFrame column of strings? 如何配置 MYSQL DB 以接受 NULL 作为 INSERT 语句中的主键,然后生成唯一的主键 - How can I configure MYSQL DB to accept NULL for primary key in the INSERT statement and then generate unique primary key 如何生成唯一的 itertools 链? - How can I generate unique itertools chains? 如何使用此 CFG 随机生成字符串? - How can I randomly generate strings with this CFG? 如何使用多线程生成数百万个唯一的字母数字字符串? - How to generate millions of unique alphanumerical strings with multithreading? 如何将2根琴弦变成一把钥匙? - How can I turn 2 strings into a key? 如何在密码学中生成相同的密钥 - How Can I Generate the same key in cryptography 如何在网页上使用列名和数据框中的唯一值生成动态选择字段? - How can I generate dynamic select fields on my webpage with column names and unique values from my dataframe? 如何使用 python 从存储在 AWS 的 Secrets Manager 中的私钥生成 OpenSSH RSA 密钥? - How can I generate OpenSSH RSA key using python from the private key stored in Secrets manager in AWS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM