简体   繁体   English

生成随机名称列表 - Python

[英]Generate list of random names - Python

I'm trying to create a list of names, with each one being different.我正在尝试创建一个名称列表,每个名称都不同。 Here is my code right now, but all it does it create multiple instances of the same name.这是我现在的代码,但它所做的一切都创建了多个同名实例。

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

full_name=random.choice(first_names)+" "+random.choice(last_names)

group=full_name*3

For example, this would show up as:例如,这将显示为:

John Smith
John Smith
John Smith

But I want something like:但我想要类似的东西:

John Williams
Andy Johnson
Joe Johnson

you're just duplicating your string here.你只是在这里复制你的字符串。 Random occurs only once.随机只出现一次。

Do it in a generator comprehension instead, and join the results with space:改为在生成器理解中执行,并将结果与​​空格连接:

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

group=" ".join(random.choice(first_names)+" "+random.choice(last_names) for _ in range(3))


print(group)

outputs:输出:

Joe Williams Joe Johnson Joe Smith

This is because you generated one name and then replicated it three times.这是因为您生成了一个名称,然后将其复制了 3 次。

If you want three different names, then loop through your choice routine three times:如果你想要三个不同的名字,那么循环你的选择例程三遍:

group = []
for i in range(3):
    full_name=random.choice(first_names)+" "+random.choice(last_names)
    group.append(full_name)
 #assuming he wants at least some kind of seperator between the names.
 group_string = ", ".join(group)

BTW, do you really want group to be all the names just concatenated?顺便说一句,您真的希望group是所有名称的连接吗?

Since the output is on one line with spaces separating each name, you can simplify the code and store every name in one list, albeit you append names in the "first name" "last name" pattern.由于输出在一行上,每个名字之间用空格隔开,因此您可以简化代码并将每个名字存储在一个列表中,尽管您在“名字”“姓氏”模式中附加了名字。 From there you can print 6 names at the end all seperated by a space, giving you three full names.从那里你可以在末尾打印 6 个名字,所有名字都用一个空格分隔,给你三个全名。 In all, the 'random.choice()' function will need to be called 6 times.总之,'random.choice()' 函数需要被调用 6 次。

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')
names = []
for i in range(3):
    names.append(random.choice(first_name)
    names.append(random.choice(last_name)

print ' '.join(names)

output输出

Andy Smith Andy Williams John Johnson

What you did is use *3 on a string which means it will copy that string three times.您所做的是在字符串上使用*3 ,这意味着它将将该字符串复制三遍。

To do what you want, you have to call random.choice for first name and for last name three times.为了做你想做的事,你必须为名字和姓氏调用random.choice三次。

full_name1 = random.choice(first_names)+" "+random.choice(last_names)
full_name2 = random.choice(first_names)+" "+random.choice(last_names)
full_name3 = random.choice(first_names)+" "+random.choice(last_names)

group = full_name1 + " " + full_name2 + " " + full_name3

You can also do that in a for loop to not duplicate the code.您也可以在for循环中执行此操作,以免重复代码。

you need to invoke random.choice() 3 times other than invoking once and print the same fullname 3 times您需要调用random.choice() 3 次,而不是调用一次并打印相同的全名 3 次

import random

first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')
group = list()
for x in range(3):
    fullname = random.choice(first_names)+" "+random.choice(last_names)
    group.add(fullname)
print " ".join(group)

... just shorter. ...只是更短。

from random import choice
first_names=('John','Andy','Joe')
last_names=('Johnson','Smith','Williams')

name = " ".join(["%s %s" % (choice(first_names), choice(last_names)) for _ in range(3)])    

there is an error for this one anyway... 反正这是一个错误...

import random
first_name=('Hyun','Rocky','Tinisha','Kristine','Jasmin','Antony','Tracie','Christeen','Tyra','Bert')
last_name=('smith','jackson','phil','connor','O-riely','shane','josh','megalbobadio','sally','patel')
names = []
for i in range(3):
    names.append(random.choice(first_name)
    names.append(random.choice(last_name)

print ' '.join(names)

You could put the main part in a function with a print in it and then call the function 3 times.您可以将主要部分放在一个带有打印的函数中,然后调用该函数 3 次。

def name():
    import random
    
    first_names=('John','Andy','Joe')
    last_names=('Johnson','Smith','Williams')
    
    full_name=random.choice(first_names)+" "+random.choice(last_names)
    print(full_name)
for _ in range(3):
    name()

pip install names pip 安装名称

Create random names with python Then to create random names just do:使用 python 创建随机名称然后创建随机名称只需执行以下操作:

import names

for i in range(10):
    print(names.get_full_name())

MY MISTAKE! 我的错!

You should try not to make the list too long-the string wont work-or at least for me it didnt names.append(random.choice(last_name) it showed an error my fault 您应该尽量不要使列表太长-字符串不起作用-或至少对我来说它没有names.append(random.choice(last_name)它显示了错误我的错

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM