use a range as a dictionary key in Python, what option do I have?(在 Python 中使用范围作为字典键,我有什么选择?)
问题描述
这是我的第一篇文章,我对编程很陌生,所以我可能无法恰当地传达我的问题,但我会尽力而为!
This is my first post and I'm quite new at programming, so I might not be able to convey my question appropriately, but I'll do my best!
tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
ub_tries = user input
tries = 1
input ('
Come on make your ' + tries_dict.get(tries) + guess: ')
这 3 个元素是我创建的猜数游戏的一部分,我将它们包含在一个 while
循环中,其中 tries += 1
在每个错误答案之后.
These 3 elements are part of a number guess game I created, and I included them in a while
loop where tries += 1
after each wrong answer.
如您所见,在我的字典中,前 4 个答案和游戏结束前最后可能的机会都有自定义值,所以这是我尝试做的:
As you can see, in my dictionary there are custom values for the first 4 answers and the last possible chance before the game is over, so here is what I tried to do:
我想找到一种方法,让第四"和最后"之间的每个答案/键都有NEXT"值.
I wanted to find a way to have the 'NEXT' value for every answer/key between 'fourth' and 'last'.
如:
tries = 5
Come on make your next guess
tries = 6
Come on make your next guess
等等
我确实找到了一些复杂循环的方法,但作为好奇的类型,我想知道更有效/实用的方法来完成这个.
I did find a way with some complex looping, but being the curious type I wanted to know of more efficient/practical ways to accomplish this.
以下是我想过但无法开始工作的一些选项:
Here are some options i thought about but couldn't get to work:
- 使用范围作为键
- 找到一种方法来生成一个值介于 4 和
ub_tries
之间的列表并将该列表用作键
- Using a range as a key
- Finding a way to generate a list with values between 4 and
ub_tries
and using that list as a key
一般来说:如何创建一种方法来为字典中未指定的键提供此一般答案(下一个或其他)?
So generally speaking: how can one create a way to have this general answer (next or whatever) for keys that aren't specified in a dictionary?
任何反馈都将不胜感激,请随时要求澄清,因为我可以告诉自己我的问题有点混乱.
Any feedback would be greatly appreciated, feel free to ask for clarifications since I can tell myself my question is kind of messy.
我希望我在编程和提出相关问题方面变得更加狡猾,到目前为止,我的编程几乎和我的总结技能一样混乱,叹息!
I hope I get more crafty both at programming and asking related questions, so far my programming is nearly as messy as my summary skills, sigh!
推荐答案
我不确定这是否是你想要的,但是 dict.get
可能是答案:
I'm not sure whether this is what you want, but dict.get
may be the answer:
>>> ub_tries = 20
>>> tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
>>> tries_dict.get(1, 'next')
'first'
>>> tries_dict.get(4, 'next')
'fourth'
>>> tries_dict.get(5, 'next')
'next'
>>> tries_dict.get(20, 'next')
'last'
>>> tries_dict.get(21, 'next')
'next'
当然,您可以以各种不同的方式将其封装在一个函数中.例如:
Of course you could wrap this up in a function, in various different ways. For example:
def name_try(try_number, ub_tries):
tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
return tries_dict.get(try_number, 'next')
无论如何,dict.get(key, default=None)
类似于 dict[key]
,除了如果 key
是不是成员,而不是引发 KeyError
,而是返回 default
.
At any rate, dict.get(key, default=None)
is like dict[key]
, except that if key
is not a member, instead of raising a KeyError
, it returns default
.
至于你的建议:
使用范围作为键??
当然,您可以这样做(如果您使用的是 Python 2 而不是 3,请使用 xrange
表示 range
),但它有什么帮助呢?
Sure, you can do that (if you're in Python 2 instead of 3, use xrange
for range
), but how would it help?
d = { range(1, 5): '???',
range(5, ub_tries): 'next',
range(ub_tries, ub_tries + 1): 'last' }
这是完全合法的——但 d[6]
会引发 KeyError
,因为 6
与 <代码>范围(5,ub_tries).
That's perfectly legal—but d[6]
is going to raise a KeyError
, because 6
isn't the same thing as range(5, ub_tries)
.
如果你想让它工作,你可以像这样构建一个 RangeDictionary
:
If you want this to work, you could build a RangeDictionary
like this:
class RangeDictionary(dict):
def __getitem__(self, key):
for r in self.keys():
if key in r:
return super().__getitem__(r)
return super().__getitem__(key)
但这远远超出了初学者的 Python",即使对于这种低效、不完整和不健壮的实现也是如此,所以我不建议这样做.
But that's well beyond "beginners' Python", even for this horribly inefficient, incomplete, and non-robust implementation, so I wouldn't suggest it.
找到一种方法来生成一个值在 4 到 ub_tries 之间的列表,并使用这样的列表作为键
finding a way to generate a list with values between 4 and ub_tries and using such list as a key
你的意思是这样的?
>>> ub_tries = 8
>>> tries_dict = {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'}
>>> tries_dict.update({i: 'next' for i in range(5, ub_tries)})
>>> tries_dict
{1: 'first', 2: 'second', 3: 'third', 4: 'fourth', 5: 'next', 6: 'next', 7: 'next', 8: 'last'}
>>> tries_dict[6]
'next'
这可行,但它可能不是一个好的解决方案.
That works, but it's probably not as good a solution.
最后,您可以使用 defaultdict
,它可以让您将默认值烘焙到字典中,而不是将其作为每次调用的一部分传递:
Finally, you could use defaultdict
, which lets you bake the default value into the dictionary, instead of passing it as part of each call:
>>> from collections import defaultdict
>>> tries_dict = defaultdict(lambda: 'next',
... {1:'first', 2:'second', 3:'third', 4:'fourth', ub_tries:'last'})
>>> tries_dict
defaultdict(<function <lambda> at 0x10272fef0>, {8: 'last', 1: 'first', 2: 'second', 3: 'third', 4: 'fourth'})
>>> tries_dict[5]
'next'
>>> tries_dict
defaultdict(<function <lambda> at 0x10272fef0>, {1: 'first', 2: 'second', 3: 'third', 4: 'fourth', 5: 'next', 8: 'last'})
但是,请注意,这会在您第一次请求时永久创建每个元素,并且您必须创建一个返回默认值的函数.这对于您要更新值并且只希望以默认值作为起点的情况更有用.
However, note that this permanently creates each element the first time you ask for it—and you have to create a function that returns the default value. This makes it more useful for cases where you're going to be updating values, and just want a default as a starting point.
这篇关于在 Python 中使用范围作为字典键,我有什么选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中使用范围作为字典键,我有什么选择?
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01