关于循环:在Python中使用范围作为字典键,我有什么选择?

use a range as a dictionary key in Python, what option do I have?

这是我的第一篇文章,我对编程很陌生,所以我可能无法恰当地表达我的问题,但我会尽力的!

1
2
3
4
5
6
7
8
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都会出现。

正如你所看到的,在我的字典中有前4个答案和游戏结束前最后一个可能的机会的自定义值,所以下面是我试图做的:

我想找到一种方法,在"fourth"和"last"之间为每个答案/键设置"next"值。

如:

1
2
3
4
5
6
7
tries = 5

Come on make your next guess

tries = 6

Come on make your next guess

等等

我确实找到了一种复杂循环的方法,但作为一个好奇的类型,我想知道更有效/更实际的方法来实现这一点。

以下是我考虑过但无法工作的一些选项:

  • 使用范围作为键
  • 找到一种生成值介于4和ub_tries之间的列表的方法,并将该列表用作键
  • 所以一般来说:如何为字典中没有指定的键创建一个通用答案(next或whatever)?

    任何反馈都会非常感谢,请随时要求澄清,因为我可以告诉自己我的问题有点混乱。

    我希望我在编程和问相关问题上变得更加狡猾,到目前为止,我的编程几乎和我的总结技能一样混乱,叹息!


    我不确定这是否是你想要的,但是dict.get可能是答案:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    >>> 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'

    当然,您可以用各种不同的方式,用一个函数来包装它。例如:

    1
    2
    3
    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

    关于你的建议:

    using a range as a key??

    当然,您可以这样做(如果您使用的是python 2而不是3,那么使用xrange来代替range,但是它有什么帮助呢?

    1
    2
    3
    d = { range(1, 5): '???',
          range(5, ub_tries): 'next',
          range(ub_tries, ub_tries + 1): 'last' }

    这是完全合法的,但是d[6]将要增加一个KeyError,因为6range(5, ub_tries)不同。

    如果你想让它工作,你可以建立一个这样的RangeDictionary

    1
    2
    3
    4
    5
    6
    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"的范围,即使对于这种效率极低、不完整且不健壮的实现,我也不建议这样做。

    finding a way to generate a list with values between 4 and ub_tries and using such list as a key

    你是说像这样?

    1
    2
    3
    4
    5
    6
    7
    >>> 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'

    这是可行的,但可能不是一个好的解决方案。

    最后,您可以使用defaultdict,它允许您将默认值烘焙到字典中,而不是将其作为每个调用的一部分传递:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    >>> 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'})

    但是,请注意,这会在您第一次请求时永久地创建每个元素,并且您必须创建一个返回默认值的函数。这使得它在您要更新值的情况下更有用,只需要一个默认值作为起点。


    可以使用范围为键的字典:

    1
    2
    3
    4
    5
    6
    def switch(x):
        return { 1<x<4: 'several', 5<x<40: 'lots'}[1]


    x = input("enter no. of monsters")
    print switch(x)

    希望这有帮助。:)


    你为什么不用清单?

    1
    2
    3
    4
    5
    6
    MAX_TRIES = 10
    tries_list = ["first","second","third","fourth"]

    for word in (tries_list[:MAX_TRIES-1] +
                 (["next"] * (MAX_TRIES - len(tries_list) - 1)) + ["last"]):
        result = raw_input("Come on, make your {} guess:".format(word))

    注意,这个代码不能像MAX_TRIES = 0计划的那样工作,但我认为这不会是问题。


    我知道你在这里的意图了吗?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    max_tries = 8

    tries_verbage = {
        1: 'first',
        2: 'second',
        3: 'third',
        4: 'fourth',
        max_tries: 'last'
        }

    for i in xrange(1, max_tries + 1):
        raw_input('Make your %s guess:' % tries_verbage.get(i, 'next'))

    收益率

    1
    2
    3
    4
    5
    6
    7
    8
    Make your first guess:1
    Make your second guess:2
    Make your third guess:3
    Make your fourth guess:4
    Make your next guess:5
    Make your next guess:6
    Make your next guess:7
    Make your last guess:8