在下面的方法定义,什么是*和**为做param2?
def foo(param1, *param2):
def bar(param1, **param2):
Run Code Online (Sandbox Code Playgroud) python syntax parameter-passing variadic-functions argument-unpacking
我有一套套装:
setlist = [s1,s2,s3...]
Run Code Online (Sandbox Code Playgroud)
我想要s1∩s2∩s3...
我可以通过执行一系列成对s1.intersection(s2)等来编写一个函数来完成它.
有推荐的,更好的或内置的方式吗?
函数参数中的星号是什么?
当我查看pickle模块时,我看到了这一点:(http://docs.python.org/3.3/library/pickle.html#pickle.dump)
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
Run Code Online (Sandbox Code Playgroud)
我知道在参数之前的单个和双星号(对于可变数量的参数),但这没有任何结果.而且我很确定这与泡菜无关.这可能就是这种情况的一个例子.我把它发送给翻译时我才知道它的名字:
>>> def func(*):
... pass
...
File "<stdin>", line 1
SyntaxError: named arguments must follow bare *
Run Code Online (Sandbox Code Playgroud)
如果重要的话,我在python 3.3.0上.
我正在尝试为python创建一个矩阵转置函数,但我似乎无法使它工作.说我有
theArray = [['a','b','c'],['d','e','f'],['g','h','i']]
Run Code Online (Sandbox Code Playgroud)
我希望我的功能能够提出来
newArray = [['a','d','g'],['b','e','h'],['c', 'f', 'i']]
Run Code Online (Sandbox Code Playgroud)
换句话说,如果我要将这个2D数组打印为列和行,我希望将行转换为列,将列转换为行.
到目前为止,我做到了这一点,但它不起作用
def matrixTranspose(anArray):
transposed = [None]*len(anArray[0])
for t in range(len(anArray)):
for tt in range(len(anArray[t])):
transposed[t] = [None]*len(anArray)
transposed[t][tt] = anArray[tt][t]
print transposed
Run Code Online (Sandbox Code Playgroud) 我无法理解这些函数的使用位置以及这些参数与普通参数的不同之处.我遇到过很多次,但从来没有机会正确理解它们.
例如:
def method(self, *links, **locks):
#some foo
#some bar
return
Run Code Online (Sandbox Code Playgroud)
我知道我可以搜索文档,但我不知道要搜索什么.
我从一个代码"层"接收一个字典,在将代码传递到另一个"层"之前执行一些计算/修改.原始字典的键和"字符串"值是unicode,但它们传递的层只接受str.
这将经常被调用,所以我想知道什么是最快的方式来转换像:
{ u'spam': u'eggs', u'foo': True, u'bar': { u'baz': 97 } }
Run Code Online (Sandbox Code Playgroud)
...至:
{ 'spam': 'eggs', 'foo': True, 'bar': { 'baz': 97 } }
Run Code Online (Sandbox Code Playgroud)
...请记住,非"字符串"值需要保留为原始类型.
有什么想法吗?
如果我有以下两组代码,我如何将它们粘合在一起?
void
c_function(void *ptr) {
int i;
for (i = 0; i < 10; i++) {
printf("%p", ptr[i]);
}
return;
}
def python_routine(y):
x = []
for e in y:
x.append(e)
Run Code Online (Sandbox Code Playgroud)
如何在x中使用连续的元素列表调用c_function?我试图将x转换为c_void_p,但这不起作用.
我也试过用类似的东西
x = c_void_p * 10
for e in y:
x[i] = e
Run Code Online (Sandbox Code Playgroud)
但是这会出现语法错误.
C代码显然需要数组的地址.我如何实现这一目标?
我np.fromfunction用来创建一个基于函数的特定大小的数组.它看起来像这样:
import numpy as np
test = [[1,0],[0,2]]
f = lambda i, j: sum(test[i])
matrix = np.fromfunction(f, (len(test), len(test)), dtype=int)
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
TypeError: only integer arrays with one element can be converted to an index
Run Code Online (Sandbox Code Playgroud) 我希望可能有一些使用理解来做到这一点,但是说我的数据看起来像这样:
data = [['a', 'b', 'c'], [1, 2, 3], [4, 5, 6]]
Run Code Online (Sandbox Code Playgroud)
我的最终目标是创建一个字典,其中第一个嵌套列表包含键,其余列表包含值:
{'a': [1, 4], 'b': [2, 5], 'c': [3, 6]}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了类似这样的东西让我接近,但是你可以告诉我在字典值中附加列表时遇到问题,这段代码只是覆盖:
d = {data[0][c]: [] + [col] for r, row in enumerate(data) for c, col in enumerate(row)}
>>> d
{'c': [6], 'a': [4], 'b': [5]}
Run Code Online (Sandbox Code Playgroud) Python中打印函数中"*"的作用是什么?
print ("Hello World!\n")
print (*"Hello World!\n")
Run Code Online (Sandbox Code Playgroud)
第一个打印功能的输出是
Hello World!
Run Code Online (Sandbox Code Playgroud)
第二个功能的输出是
H e l l o W o r l d !
Run Code Online (Sandbox Code Playgroud)
但是在python 2.7中它不起作用!
我正在研究这个链接,这是代码.
U1 = np.random.rand(*H1.shape) < p # first dropout mask
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时为什么会失败?
import numpy
numpy.random.rand(*1) < 2
Run Code Online (Sandbox Code Playgroud)
我理解该rand()函数接受一个数字,这就是为什么我感到困惑的是代码应该工作.