可能重复:
Javascript语法:逗号是什么意思?
我在阅读本文时遇到了代码(执行Ctrl+ F搜索Andre Breton):
//function returning array of `umbrella` fibonacci numbers
function Colette(umbrella) {
var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon;
Array.prototype.embrace = [].push;
while(2 + staircase++ < umbrella) {
bassoon = galleons + brigantines;
armada.embrace(brigantines = (galleons = brigantines, bassoon));
}
return armada;
}
Run Code Online (Sandbox Code Playgroud)
什么是x = (y = x, z)结构是什么意思?或者更具体地说,是什么y = x, z意思?我称之为逗号分配,因为它看起来像赋值并且有逗号.
在Python中,它意味着元组解包(或在这种情况下打包).这是同样的情况吗?
javascript variable-assignment assignment-operator assign iterable-unpacking
假设我有一个像这样的方法定义:
def myMethod(a, b, c, d, e)
Run Code Online (Sandbox Code Playgroud)
然后,我有一个变量和这样的元组:
myVariable = 1
myTuple = (2, 3, 4, 5)
Run Code Online (Sandbox Code Playgroud)
有没有办法可以通过爆炸元组爆炸,以便我可以将其成员作为参数传递?像这样的东西(虽然我知道这不会起作用,因为整个元组被认为是第二个参数):
myMethod(myVariable, myTuple)
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我想避免单独引用每个元组成员...
是否有内置函数/运算符可用于从字典中解压缩值并将其分配给实例变量?
这就是我打算做的事情:
c = MyClass()
c.foo = 123
c.bar = 123
# c.foo == 123 and c.bar == 123
d = {'bar': 456}
c.update(d)
# c.foo == 123 and c.bar == 456
Run Code Online (Sandbox Code Playgroud)
类似于字典的东西,update()它从另一个字典加载值但是对于普通对象/类实例?
在Python中我可以写
def myMethod():
#some work to find the row and col
return (row, col)
row, col = myMethod()
mylist[row][col] # do work on this element
Run Code Online (Sandbox Code Playgroud)
但是在C#中,我发现自己在写作
int[] MyMethod()
{
// some work to find row and col
return new int[] { row, col }
}
int[] coords = MyMethod();
mylist[coords[0]][coords[1]] //do work on this element
Run Code Online (Sandbox Code Playgroud)
Pythonic方式显然更加清洁.有没有办法在C#中做到这一点?
是否有一个干净/简单的方法从右到右从右侧解压缩Python元组?
例如
j = 1,2,3,4,5,6,7
(1,2,3,4,5,6,7)
v,b,n = j[4:7]
Run Code Online (Sandbox Code Playgroud)
我可以修改切片表示法v = j[6], b=j[5], n=j[4]吗?
我意识到我可以命令左侧获得所需的元素,但可能存在我只想从左到右解包元组的情况.
正如PythonCookbook中提到的,*可以在元组之前添加,*这里的意思是什么?
第1.18章.将名称映射到序列元素:
from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price'])
s = Stock(*rec)
# here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45)
Run Code Online (Sandbox Code Playgroud)
在同一部分,**dict提出:
from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])
# Create a prototype instance
stock_prototype = Stock('', 0, 0.0, None, None)
# Function to convert a dictionary to a Stock
def dict_to_stock(s):
return stock_prototype._replace(**s)
Run Code Online (Sandbox Code Playgroud)
什么是**这里的功能?
昨天我在Python 2和Python 3之间遇到了这个奇怪的解包差异,并且在快速谷歌搜索之后似乎没有找到任何解释.
Python 2.7.8
a = 257
b = 257
a is b # False
a, b = 257, 257
a is b # False
Run Code Online (Sandbox Code Playgroud)
Python 3.4.2
a = 257
b = 257
a is b # False
a, b = 257, 257
a is b # True
Run Code Online (Sandbox Code Playgroud)
我知道它可能不会影响程序的正确性,但它确实让我有点烦恼.任何人都可以在拆包时给出一些关于这种差异的见解吗?
在Python中,我可以这样做:
t = (1, 2)
a, b = t
Run Code Online (Sandbox Code Playgroud)
......并且a将为1,b将为2.假设我'(1 2)在Scheme中有一个列表.有没有办法做类似的事情let?如果它有所作为,我正在使用Racket.
拆包/图示运营商*和**在横跨Python版本(2.7,3.X <3.5和3.x> = 3.5)其适用性广泛不同.
例如:
| 2.7 | 3.1-3.4 | 3.5
----------------------------------------------------------------------
function(*args) ? ? ?
x, *y, z = [1, 2, 3, 4, 5] x ? ?
{**x, **y} x x ?
Run Code Online (Sandbox Code Playgroud)
我错过了各种版本之间是否还有其他差异?我正在浏览PEP和Readmes,但文档并未详细说明.
python function parameter-passing argument-unpacking iterable-unpacking
在Python中,假设定义了以下函数:
def function(a, b, c):
... do stuff with a, b, c ...
Run Code Online (Sandbox Code Playgroud)
我可以使用Python的序列解包来使用该函数:
arguments = (1, 2, 3)
function(*arguments)
Run Code Online (Sandbox Code Playgroud)
Common Lisp中是否存在类似的功能?如果我有一个功能:
(defun function (a b c)
... do stuff with a, b, c ...
Run Code Online (Sandbox Code Playgroud)
如果我有3个元素的列表,我可以轻松地使用这3个元素作为函数的参数?
我目前实现它的方式如下:
(destructuring-bind (a b c) (1 2 3)
(function a b c))
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
python ×7
tuples ×4
python-3.x ×3
list ×2
assign ×1
c# ×1
common-lisp ×1
difference ×1
function ×1
javascript ×1
lisp ×1
namedtuple ×1
parameters ×1
python-2.7 ×1
racket ×1
scheme ×1