我需要有关参数的帮助.这两个函数定义是否对print_twice执行完全相同的操作?
def print_twice(lol):
print lol
print lol
def print_twice(michael):
print michael
print michael
Run Code Online (Sandbox Code Playgroud)
如果是,那么我猜测用于参数的单词无关紧要,对吗?
我有以下 HTML 段,我想将其转换为 django 脆皮形式:
<div class="span5">
<h3 class='offset1'>Select images</h3>
<div id='image-drop' class='dropzone span5 center'>
<p>Drag and drop photos and video here</p>
<p class='big'>+</p>
<p>Or click to add using the file browser</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我将如何包含标签和
标签?我试过这个:
self.helper.layout = Layout(
Div(css_class='span5',
HTML("<h3 class='offset1'>Select Images</h3>"),
Div(css_id='image-drop',
css_class='dropzone span5 center',
HTML("<p>Drag and drop photos and video here</p><p class='big'>+</p><p>Or click to add using the file browser</p>")
)
)
)
Run Code Online (Sandbox Code Playgroud)
但这给出了一个 SyntaxError: non-keyword arg after keyword arg --> 指向带有 HTML 字段的行。在 Div 中包含 HTML 有什么问题吗?如何翻译给定的 …
我正在学习《面向绝对初学者的 Python 编程》这本书,并决定通过制作自己的游戏来测试我的一些技能。游戏基本上是“不要被飞来的尖刺击中”,我遇到了一个问题。使用此代码运行时:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
Run Code Online (Sandbox Code Playgroud)
我没问题。但是当我想使用“ init ”时,就像在这段代码中一样:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to …Run Code Online (Sandbox Code Playgroud) 数据框如下所示:
d = {'ID': [1, 2,3],'V':['F','G','H'],'AAA':[0,1,1],'AA':[0,2,2],'A':[0,3,3],'BBB':[0,4,4]}
df2 = pd.DataFrame(data=d)
Run Code Online (Sandbox Code Playgroud)
和字典看起来像这样:
dct ={1:{'F':[2,3,5],'G':[3,5,6],'H':[6,7,8]},
2:{'F':[1,3,5],'G':[8,5,6],'H':[9,7,8]},
3:{'F':[5,3,5],'G':[4,5,6],'H':[10,7,8]}
}
Run Code Online (Sandbox Code Playgroud)
根据“ID”和“V”的值,我可以访问字典中的列表,即 dct[2]['G']。如何对此应用合并?
简而言之,我想将特定列表作为一行附加到数据框。
预期结果应如下所示:
op_d = {'ID': [1, 2,3],'V':['F','G','H'],'AAA':[0,1,1],'AA':[0,2,2],'A':[0,3,3],'BBB':[0,4,4],'Q1':[2,8,10],'Q2':[3,5,7],'Q3':[5,6,8]}
output_df = pd.DataFrame(data=op_d )
Run Code Online (Sandbox Code Playgroud) 我正在阅读《Python 编程》,无法弄清楚以下代码中 **D 的含义:
>>> D = {'say': 5, 'get': 'shrubbery'}
>>> '%(say)s => %(get)s' % D
'5 => shrubbery'
>>> '{say} => {get}'.format(**D)
'5 => shrubbery'
Run Code Online (Sandbox Code Playgroud)
我用 google 搜索了 python 中的 **kwargs,大多数结果都在讨论让函数接受任意数量的关键字参数。
这里的 string.format(**D) 看起来不像是让函数接受任意数量的关键字参数的东西,因为我看到字典类型变量 D 只是一个参数。但这是什么意思呢?
我在random.randint()中使用*感到困惑,找不到任何文档
random.randint( *(1,300) )
Run Code Online (Sandbox Code Playgroud)
与
random.randint( 1,300 )
random.randint( *300 )
TypeError: randint() argument after * must be a sequence, not int
Run Code Online (Sandbox Code Playgroud) 它假设是功能组合.我认为问题是当funcs中只剩下一个函数时.我希望它是一个空元组,但它没有像那样识别并进入无限循环
谢谢!:)
def compose(*funcs):
if len(funcs)==0:
return lambda x:x
f=funcs[0]
return lambda x: f(compose(funcs[1:])(x))
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用字典的值进行函数调用。
该函数采用许多参数,大多数具有默认值。
def foo(name, a=None, b='', c=12):
print(name,a,b,12)
Run Code Online (Sandbox Code Playgroud)
如果字典已完全填充,函数调用将如下所示。
def call_foo(arg_dict):
foo(name=arg_dict['name'], a=arg_dict['a'], b=arg_dict['b'], c=arg_dict['c'])
Run Code Online (Sandbox Code Playgroud)
不过,我需要根据这些键是否实际存在于字典中来进行函数调用。因此,如果只有一部分参数存在,我只会传递这些参数。
def call_foo(arg_dict):
if 'a' in arg_dict and 'b' in arg_dict and 'c' in arg_dict:
foo(name=arg_dict['name'], a=arg_dict['a'], b=arg_dict['b'], c=arg_dict['c'])
elif 'a' in arg_dict and 'c' in arg_dict:
foo(name=arg_dict['name'], a=arg_dict['a'], c=arg_dict['c'])
Run Code Online (Sandbox Code Playgroud)
这种类型的表达式将很快变得难以管理,因为有大量可选参数。
如何定义要传递给 foo 的命名参数列表?类似于以下内容。
def call_foo(arg_dict):
arg_list = []
arg_list.append(name=arg_dict['name'])
if 'a' in arg_dict:
arg_list.append(a=arg_dict['a'])
if 'b' in arg_dict:
arg_list.append(b=arg_dict['b'])
if 'c' in arg_dict:
arg_list.append(c=arg_dict['c'])
foo(arg_list)
Run Code Online (Sandbox Code Playgroud) 我还是python的新手; 所以,对于这个有些模糊的问题感到抱歉.我只是好奇是否可以为参数添加多个输入.例如:
def censored(sentence, word):
if word in sentence:
sentence = sentence.replace(word, "*" * len(word))
return sentence
print censored("foo off", "foo")
Run Code Online (Sandbox Code Playgroud)
这将打印出"****off".这就是我想要的; 但是,如果我想添加除"foo"之外的其他输入,该怎么办?
有没有其他方法可以做到这一点,而无需在函数中添加第三,第四和第n个参数?
我正在尝试spread在Python 3.6中编写一个函数(我不能使用任何较新的版本),到目前为止,我已经有了类似以下内容的东西:
d = {"a": 1, "b": 2, "c": 3}
a, b, c = spread(d, ['a', 'b', 'c'])
a
>> 1
b
>> 2
c
>> 3
Run Code Online (Sandbox Code Playgroud)
问题是:存在某种重复,因为左侧的位置必须与函数第二个参数上的键列表匹配才能使其有意义。因此,更改键列表的顺序,变量a将拥有与相比不同的值d['a']。我需要保持一致性
a, b, c = spread(d) # how?
Run Code Online (Sandbox Code Playgroud)
或者spread(d, ???)。我不考虑初始化a, b, c,None然后将它们作为列表传递。
关于如何解决这个问题有什么想法或线索吗?可能吗?谢谢!
我希望以下列方式显示输出keys: values:
dad:bob
Mom:lisa
brother : joe
& so on
Run Code Online (Sandbox Code Playgroud)
但只有值显示在输出中.
我应该在此代码中进行哪些更改才能获得所需的输出?
d = dict(Dad='Bob', Mom='Lisa', Brother= 'joe')
def f2(Dad,Mom,Brother):
print Dad,Mom,Brother
f2(**d)
Run Code Online (Sandbox Code Playgroud) def Foo( *args, **kargs ):
Run Code Online (Sandbox Code Playgroud)
我知道 **kargs 可以接受多个参数。但是,函数方法有 *args 和 **kargs 是怎么回事?如何在现实世界中使用它?
谁能给我一个例子?谢谢!
我有一个看似简单的问题,但我找不到答案。使用一个简单的函数,例如:
def test_kwargs_1(a,**kwargs):
print a
print b
Run Code Online (Sandbox Code Playgroud)
我在想,如果我通过了:
kwargs = {'a':1,'b':2}
test_kwargs_1(**kwargs)
Run Code Online (Sandbox Code Playgroud)
它会打印:
1
2
Run Code Online (Sandbox Code Playgroud)
因为它会解压 "kwargs" 并且变量 "a" 和 "b" 都可用。相反,我得到:
1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
.
.
----> 3 print b
NameError: global name 'b' is not defined
Run Code Online (Sandbox Code Playgroud)
我知道“b”是一个可能存在也可能不存在的变量,但我认为如果在 kwargs 中明确定义,解包 kwargs 将使变量“b”可用。我没有得到什么?谢谢,s