【问题标题】:is there an easy way to bring a dictionary into a function?有没有一种简单的方法可以将字典带入函数中?
【发布时间】:2020-02-15 23:26:10
【问题描述】:

我们的目标是玩这个。

我创建了一个字典“all_dice”,并在使用之前将它传递给了 3 个函数。

必须有一种更简单的方法来访问字典,而不必像这样一遍又一遍地传递它。

import random
from random import randint

x = 0
the_dice_chosen = ""

##this is the dictionary

all_dice = {"six": 
        [["_____",],
        ["0   0",],
        ["0   0",],
        ["0   0",],
        ["_____",],],
        "five": 
        [["_____",],
        ["0   0",],
        ["  0  ",],
        ["0   0",],
        ["_____",],],
        "four": 
        [["_____",],
        ["0   0",],
        ["     ",],
        ["0   0",],
        ["_____",],],
        "three": 
        [["_____",],
        ["0    ",],
        ["  0  ",],
        ["    0",],
        ["_____",],],
        "two": 
        [["_____",],
        ["0    ",],
        ["    ",],
        ["    0",],
        ["_____",],],
        "one": 
        [["_____",],
        ["     ",],
        ["  0  ",],
        ["     ",],
        ["_____",],],}

## This prints the dictionary of the numbers chosen side by side.
## Again "all_dice" is passed as "ad" and is used.

def dice_print(ndy,ndz,ad):
    x = 0
    nday = ad[ndy]
    ndaz = ad[ndz]
    for i in nday:
        print(nday[x], ndaz[x])
        x = x + 1

##This creates the random dice numbers.       

def dice_roller():
    x = randint(1, 6) 
    y = randint(1, 6) 
    return(x, y)

## This converts the numbers into selections in the dictionary. E.G. 6 into "six" "all_dice" is ad
## and it will from now on be passed as "ad".

def dice_maker(ad,):
    master = {1 : "one",
              2 : "two",
              3 : "three",
              4 : "four",
              5 : "five",
              6 : "six",}
    for i in range(1,2):
        x = dice_roller()
        y = int(x[0])
        z = int(x[1])
        new_die_y = (master[y])
        new_die_z = (master[z])
        dice_print(new_die_y,new_die_z,ad)


##This calls the script to action and passing the dictionary "all_dice"

dice_maker(all_dice)```

【问题讨论】:

  • 你的字典是全球性的,你可以在任何地方直接访问它。
  • 所以我一直都在想。叹。非常感谢。 :D
  • 您可能想在下面查看我的答案,了解有关压缩代码的一些提示。此外,在 all_dice 中表示骰子的方式可以更简单地修改,方法是使骰子成为字符串列表而不是字符串列表。
  • 通过重组你的 all_dice 字典,我能够用四行代码将你的代码重写为一个函数。

标签: python python-3.x function dictionary


【解决方案1】:

您完全可以将其作为参数丢弃。

python 模块的行为与单例类非常相似。一个模块,就像一个类,是一个对象。 all_dice 是模块的一个属性,因此作用域为同一模块的函数可以访问。

import random
from random import randint

x = 0
the_dice_chosen = ""

##this is the dictionary

all_dice = {"six": 
        [["_____",],
        ["0   0",],
        ["0   0",],
        ["0   0",],
        ["_____",],],
        "five": 
        [["_____",],
        ["0   0",],
        ["  0  ",],
        ["0   0",],
        ["_____",],],
        "four": 
        [["_____",],
        ["0   0",],
        ["     ",],
        ["0   0",],
        ["_____",],],
        "three": 
        [["_____",],
        ["0    ",],
        ["  0  ",],
        ["    0",],
        ["_____",],],
        "two": 
        [["_____",],
        ["0    ",],
        ["    ",],
        ["    0",],
        ["_____",],],
        "one": 
        [["_____",],
        ["     ",],
        ["  0  ",],
        ["     ",],
        ["_____",],],}

## This prints the dictionary of the numbers chosen side by side.
## Again "all_dice" is passed as "ad" and is used.

def dice_print(ndy,ndz):
    x = 0
    nday = all_dice[ndy]
    ndaz = all_dice[ndz]
    for i in nday:
        print(nday[x], ndaz[x])
        x = x + 1

##This creates the random dice numbers.       

def dice_roller():
    x = randint(1, 6) 
    y = randint(1, 6) 
    return(x, y)

## This converts the numbers into selections in the dictionary. E.G. 6 into "six" "all_dice" is ad
## and it will from now on be passed as "ad".

def dice_maker():
    master = {1 : "one",
              2 : "two",
              3 : "three",
              4 : "four",
              5 : "five",
              6 : "six",}
    for i in range(1,2):
        x = dice_roller()
        y = int(x[0])
        z = int(x[1])
        new_die_y = (master[y])
        new_die_z = (master[z])
        dice_print(new_die_y,new_die_z)


##This calls the script to action and passing the dictionary "all_dice"

dice_maker()

global 关键字

如果您需要在函数中重新分配 all_dice。您必须将global all_dice 添加到函数中。

all_dice = None

def set_all_dice(value):
    global all_dice
    all_dice = value

def _set_all_dice(value):
    all_dice = value       ## all_dice here is scoped to the function

【讨论】:

  • 嗯,这不是 Python 中类的工作方式,所以这是一个误导性的陈述。因此,类范围内的名称该类中的方法无法访问
  • @juanpa.arrivillaga 这就是我说 singleton 类的原因。单例类的对象具有属性,并且这些属性对于所有对象引用都是相同的。模块也是如此。当您以不同的方式导入模块时,该模块具有属性,并且这些属性对于该模块的所有导入都是相同的。然而,在这两种情况下,我们都在谈论对象属性,并且从此类对象的用户的角度来看,它们的行为是相同的。 可能单例类对象会更好用。我让你判断。
  • @PedroRodrigues 如果我将其声明为全局则不会 =)
  • 我只是想幽默@juanpa.arrivillaga,如果这就是你所指的。
  • 哦.. 你说的是必须使用 self 之间的区别。访问类成员与模块函数中的全局关键字。
【解决方案2】:

你可以压缩很多代码。可以使用 ''.join() 和列表推导来压缩打印的函数。 dice_roller() 函数只有几行活动代码,可以直接包含在主函数中。

>>> def roll():
...     num_words = ["dummy", "one", "two", "three", "four", "five", "six"]
...     x = randint(1, 6)
...     y = randint(1, 6)
...     d1 = all_dice[num_words[x]]
...     d2 = all_dice[num_words[y]]
...     roll_str = ''.join(["%s %s\n" % (d1[i][0], d2[i][0]) for i in range(5)])
...     print(roll_str)
>>> roll()
_____ _____
0     0   0
        0  
    0 0   0
_____ _____

通过像这样重新实现 all_dice:

all_dice = {6: ["____", "0  0", "0  0", "0  0", "____"], 5: <and so on...>

代码进一步简化:

>>> def roll():
...     d1 = all_dice[randint(1, 6)]
...     d2 = all_dice[randint(1, 6)]
...     print(''.join(["%s %s\n" % (d1[i], d2[i]) for i in range(5)]))
>>> roll()
_____ _____
0     0   0
        0  
    0 0   0
_____ _____

【讨论】:

    猜你喜欢
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2013-02-21
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多