使用python创建一个简单的寄存器 [英] making a simple register using python

查看:693
本文介绍了使用python创建一个简单的寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望将人名称并存储到寄存器中。 
检查名称是否允许,仅允许使用英文名称。
为数组添加名称,即寄存器。
并且能够通过键入函数retrieve_name(name)来检索名称。
如果检索名称==数组中的字符串,则返回名称已经在注册表中





我是python的新手,而不是一个经验丰富的程序员,所以这是我如何尝试对人名进行注册的思考过程。如果您认为有更好的方法可以做到这一点,那就继续吧。也许你用对象来创建这个,如果是这样那就好了:)



关于我的一点点......

我计划在未来更深入地进行这项工作,但是现在想保持简单,以便我能理解它。我正在通过python上的codecademy教程工作。

解决方案

有很多方法可以做到这一点,但最合适的方法是使用字典(也称为关联数组): http://docs.python.org/2 /tutorial/datastructures.html#dictionaries [ ^ ]。



这里假设最终你需要将名称存储为字典键和其他东西,一些与名称相关的信息,否则这样的存储会完全无用的。但是,如果您只想存储没有重复项的对象,那么适当的Python结构将是 set http://docs.python.org/2/tutorial/datastructures.html#sets [ ^ ]。



-SA

We want to take peoples name and store it into a register.
Check if the name is allowed, only English names are allowed.
Add names to a array i.e the register.
And be able to retrieve there name by typing a function retrieve_name(name).
If retrieve name == a string in the array, then return "name is already in the register"



I am new to python and not a experienced programmer so this is my thought process on how i would try to make a register of peoples names. If you think there is a better way to do this then go ahead. Maybe you use objects to create this, if so then thats fine :)

A little bit about me....
I plan on making this more in depth in the future but would like to keep it simple for now so that i can understand it. I am currently working my way through codecademy tutorials on python.

解决方案

There are many ways to do this, but the most adequate way is using a dictionary (also known as "associative array"): http://docs.python.org/2/tutorial/datastructures.html#dictionaries[^].

Here is assume that ultimately you would need to store names as dictionary keys and something else, some information related to names, otherwise such storage would be totally useless. However, if you wanted to store just objects without duplicates, the adequate Python structure would be a set: http://docs.python.org/2/tutorial/datastructures.html#sets[^].

—SA


这篇关于使用python创建一个简单的寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆