Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过组合“名字”和“姓氏”数组中的值来创建唯一的名称_Python_Python 3.x - Fatal编程技术网

Python 通过组合“名字”和“姓氏”数组中的值来创建唯一的名称

Python 通过组合“名字”和“姓氏”数组中的值来创建唯一的名称,python,python-3.x,Python,Python 3.x,我有两个数组,我想通过组合这两个数组来创建一个名称。 但是,不应该有重复 除了查找名称结果值数组的递归方式外,我是否可以像返回随机名称一样返回随机名称 最简单的方法是将随机生成的名称存储在列表中,并在每次创建新名称时查找列表,如下所示 随机导入 名字=[“托尼”、“亚当”、“阿古斯丁”、“布兰登”、“斯坦”] 姓氏=['smith','stark','wlliams'] 全名重复列表=[] def生成完整名称(): 全名=random.choice(名字)+“”+random.choice(姓氏

我有两个数组,我想通过组合这两个数组来创建一个名称。 但是,不应该有重复

除了查找名称结果值数组的递归方式外,我是否可以像返回随机名称一样返回随机名称

最简单的方法是将随机生成的名称存储在列表中,并在每次创建新名称时查找列表,如下所示

随机导入
名字=[“托尼”、“亚当”、“阿古斯丁”、“布兰登”、“斯坦”]
姓氏=['smith','stark','wlliams']
全名重复列表=[]
def生成完整名称():
全名=random.choice(名字)+“”+random.choice(姓氏)
如果全名列表中的全名:
生成\u全名()
其他:
全名重复列表。追加(全名)
打印(全名)
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
结果:
托尼·斯塔克
托尼·维利亚姆斯
布兰登·斯塔克
布兰登·史密斯
亚当·斯塔克
阿古斯丁·斯塔克
阿古斯丁·史密斯
斯坦·史密斯
每个结果都是不重复的全名

但是,我不想使用检查已创建数组的方法。 因为数组的大小会增加,返回值的数量也会增加,所以函数可能会在出现问题时继续递归执行

下面是另一个计划: 像2个深度的语句

随机导入
名字=[“托尼”、“亚当”、“阿古斯丁”、“布兰登”、“斯坦”]
姓氏=['smith','stark','wlliams']
类索引():
idx_first=0
idx_last=0
def生成完整名称():
全名=第一名[Index.idx\u first]+“”+姓氏[Index.idx\u last]
打印(全名)
Index.idx_last=Index.idx_last+1
如果Index.idx_last==len(last_name)-1:
Index.idx_first=Index.idx_first+1
Index.idx_last=0
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
生成\u全名()
结果:
托尼·史密斯
托尼·斯塔克
亚当·斯密
亚当·斯塔克
阿古斯丁·史密斯
阿古斯丁·斯塔克
布兰登·史密斯
但这似乎不是随机的


问题:我可以创建一个输出全名列表(如random)的函数,而不是检查现有的全名数组吗?

简单的方法是使用哈希表:每个字符串都用一个易于恢复的哈希代码索引。Python将很容易为您做到这一点:将名称放入
集合
,然后检查您生成的新名称是否已经在集合中。做出新的选择,直到你得到一个你还没有用过的

full_name_dup_set = set()

def generate_full_name():

    full_name = random.choice(first_name) + " " + \
                random.choice(last_name)

    while full_name in full_name_dup_set:
        full_name = random.choice(first_name) + " " + \
                    random.choice(last_name)

    full_name_dup_set.add(full_name)
    print(full_name)

要做到这一点,最简单的方法是使用哈希表:每个字符串都使用易于恢复的哈希代码进行索引。Python将很容易为您做到这一点:将名称放入
集合
,然后检查您生成的新名称是否已经在集合中。做出新的选择,直到你得到一个你还没有用过的

full_name_dup_set = set()

def generate_full_name():

    full_name = random.choice(first_name) + " " + \
                random.choice(last_name)

    while full_name in full_name_dup_set:
        full_name = random.choice(first_name) + " " + \
                    random.choice(last_name)

    full_name_dup_set.add(full_name)
    print(full_name)

一个选项是拥有一组副本,并使用生成器:

import random

first_name = ['tony', 'adam', 'agustin', 'branden','stan']
last_name = ['smith', 'stark',  'wlliams']

def generate_random_names(first, last):
    duplicates = set()
    while True:
        f = random.choice(first)
        l = random.choice(last)
        if (f, l) in duplicates:
            continue
        duplicates.add((f, l))
        yield f, l

for i, (f, l) in zip(range(1, 11), generate_random_names(first_name, last_name)):
    print('{}. {} {}'.format(i, f, l))
印刷品:

1. stan wlliams
2. adam wlliams
3. tony wlliams
4. adam stark
5. tony stark
6. branden wlliams
7. stan stark
8. agustin smith
9. branden stark
10. agustin wlliams
[('stan', 'wlliams'), ('tony', 'stark'), ('agustin', 'smith'), ('agustin', 'wlliams'), ('tony', 'smith'), ('tony', 'wlliams'), ('stan', 'stark'), ('branden', 'stark'), ('stan', 'smith'), ('branden', 'wlliams')]
另一个版本正在使用
itertools.product
random.sample

import random
from itertools import product

first_name = ['tony', 'adam', 'agustin', 'branden','stan']
last_name = ['smith', 'stark',  'wlliams']

print(random.sample([*product(first_name, last_name)], 10))
印刷品:

1. stan wlliams
2. adam wlliams
3. tony wlliams
4. adam stark
5. tony stark
6. branden wlliams
7. stan stark
8. agustin smith
9. branden stark
10. agustin wlliams
[('stan', 'wlliams'), ('tony', 'stark'), ('agustin', 'smith'), ('agustin', 'wlliams'), ('tony', 'smith'), ('tony', 'wlliams'), ('stan', 'stark'), ('branden', 'stark'), ('stan', 'smith'), ('branden', 'wlliams')]

一个选项是拥有一组副本,并使用生成器:

import random

first_name = ['tony', 'adam', 'agustin', 'branden','stan']
last_name = ['smith', 'stark',  'wlliams']

def generate_random_names(first, last):
    duplicates = set()
    while True:
        f = random.choice(first)
        l = random.choice(last)
        if (f, l) in duplicates:
            continue
        duplicates.add((f, l))
        yield f, l

for i, (f, l) in zip(range(1, 11), generate_random_names(first_name, last_name)):
    print('{}. {} {}'.format(i, f, l))
印刷品:

1. stan wlliams
2. adam wlliams
3. tony wlliams
4. adam stark
5. tony stark
6. branden wlliams
7. stan stark
8. agustin smith
9. branden stark
10. agustin wlliams
[('stan', 'wlliams'), ('tony', 'stark'), ('agustin', 'smith'), ('agustin', 'wlliams'), ('tony', 'smith'), ('tony', 'wlliams'), ('stan', 'stark'), ('branden', 'stark'), ('stan', 'smith'), ('branden', 'wlliams')]
from collections import deque

firstCount = len(first_name)
lastCount  = len(last_name)
fullCount  = firstCount*lastCount
names = deque(random.sample(range(fullCount),fullCount))

def randomFullName():
    names.rotate(1)
    i = names[0]
    iFirst = i // lastCount
    iLast  = i % lastCount
    return first_name[iFirst] + " " + last_name[iLast]

for _ in range(10): 
    print(randomFullName())
def main():

    from random import sample

    first_names = ["Bob", "Tom", "Jay"]
    last_names = ["Jones", "Watson", "Smith"]

    all_possible_names = [f"{first} {last}" for first in first_names for last in last_names]

    print(sample(all_possible_names, k=4))

    return 0

if __name__ == "__main__":
    import sys
    sys.exit(main())