栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

利用python随机生成姓名

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用python随机生成姓名

前段时间做了一个数据库的课设,其中姓名的初始数据引起了我的思考,于是下面的用于随机生成姓名的代码便诞生了:

import random
import os


class CreateName:
    """生成名字"""

    def __init__(self, numbers=100):
        # 用来存姓氏
        self.first_names = []
        # 用来存名,包含双字
        self.last_names = []
        # 标志位
        self.sign = True
        # 用来存最终的名字
        self.names = []
        # 生成名字数
        self.numbers = numbers

    def create_first_name(self):
        """将百家姓做成一个列表"""
        # 拼接文件路径
        with open(os.path.dirname(
                os.path.realpath(__file__)) + '/the_first_name', 'r',
                  encoding='UTF-8') as f:
            # 获取百家姓
            the_hundred_surnames = f.readlines()
            # 先处理单姓,第51行之前的都是单性
            for the_hundred_surname in the_hundred_surnames[:51]:
                for the_surname in the_hundred_surname[
                                   :len(the_hundred_surname) - 1]:
                    self.first_names.append(the_surname)
            # 再处理复姓,将每一个字取出来,然后拼成复姓再放回去
            for the_hundred_surname in the_hundred_surnames[51:]:
                for the_surname in the_hundred_surname[
                                   :len(the_hundred_surname) - 1]:
                    # 判断,取第一个字时先存着,等第二个字到来
                    if self.sign:
                        temporary_str = the_surname
                        self.sign = False
                        continue
                    # 第二个字到来后与第一个字拼在一起
                    self.first_names.append(temporary_str + the_surname)
                    self.sign = True
                    temporary_str = ''

    def create_last_name(self):
        """随便找一篇课文或者汉字列表当名"""
        # 用来存单名
        singular_last_names = []
        # 生成名的个数
        number = 1000
        with open(os.path.dirname(
                os.path.realpath(__file__)) + '/the_last_name.txt', 'r',
                  encoding='UTF-8') as f:
            # 获取汉字列表
            chinese_characters = f.readlines()
            # 先将所有的汉字组成单字列表
            for chinese_character in chinese_characters:
                for personal_name in chinese_character[
                                     :len(chinese_character) - 1]:
                    singular_last_names.append(personal_name)
            # 用random模块生成单字有双字混合的名
            while number != 0:
                choice = random.randint(1, 2)
                temporary_list = random.choices(singular_last_names,
                                                k=choice)
                # 判断,如果随机出来是1就是单字,2就是双字,然后去列表中去取
                if len(temporary_list) == 1:
                    # 如果存在就跳过,否则就加入
                    if temporary_list[0] not in self.last_names:
                        self.last_names.append(temporary_list[0])
                        number -= 1
                else:
                    # 如果存在就跳过,否则就加入
                    if temporary_list[0] + temporary_list[
                        1] not in self.last_names:
                        self.last_names.append(
                            temporary_list[0] + temporary_list[1])
                        number -= 1

    def create_name(self):
        """生成姓名"""
        # 先运行创建名和姓的代码
        self.create_first_name()
        self.create_last_name()
        # 对姓和名进行随机组合,并去重
        while self.numbers != 0:
            # 用来临时存放生成的姓名
            result = random.choices(self.first_names)[0] + random.choices(
                self.last_names)[0]
            if result not in self.names:
                self.names.append(result)
                self.numbers -= 1
        # 将名字列表返回
        return self.names


if __name__ == "__main__":
    print(CreateName().create_name())

姓:https://download.csdn.net/download/wxbjwan/19846290

名:https://download.csdn.net/download/wxbjwan/19846701

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号