我有一个元组元组 - 例如:
tupleOfTuples = ((1, 2), (3, 4), (5,))
Run Code Online (Sandbox Code Playgroud)
我想按顺序将其转换为所有元素的平面一维列表:
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
我一直试图通过列表理解来实现这一目标.但我似乎无法弄明白.我能够通过for-each循环完成它:
myList = []
for tuple in tupleOfTuples:
myList = myList + list(tuple)
Run Code Online (Sandbox Code Playgroud)
但我觉得必须有一种方法可以通过列表理解来做到这一点.
一个简单的[list(tuple) for tuple in tupleOfTuples]只是给你一个列表列表,而不是单个元素.我想我可以通过使用解包操作符然后解压缩列表来构建它,如下所示:
[*list(tuple) for tuple in tupleOfTuples]
Run Code Online (Sandbox Code Playgroud)
要么
[*(list(tuple)) for tuple in tupleOfTuples]
Run Code Online (Sandbox Code Playgroud)
......但那没用.有任何想法吗?或者我应该坚持循环?
是否可以在Python 2中模拟扩展元组解包?
具体来说,我有一个for循环:
for a, b, c in mylist:
Run Code Online (Sandbox Code Playgroud)
当mylist是大小为3的元组列表时,它工作正常.如果我传入一个大小为四的列表,我希望循环工作相同.
我想我最终会使用命名元组,但我想知道是否有一种简单的写法:
for a, b, c, *d in mylist:
Run Code Online (Sandbox Code Playgroud)
这样可以d吃掉任何额外的成员.
为什么python只允许命名参数跟随函数调用中的元组解包表达式?
>>> def f(a,b,c):
... print a, b, c
...
>>> f(*(1,2),3)
File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
Run Code Online (Sandbox Code Playgroud)
它只是一种美学选择,还是存在允许这会导致一些含糊不清的情况?
>>> LOL = [[1, 2], ['three']]
>>> [*LOL[0], *LOL[1]]
[1, 2, 'three']
Run Code Online (Sandbox Code Playgroud)
好的!再见itertools.chain.反正从来没有喜欢过你.
>>> [*L for L in LOL]
File "<ipython-input-21-e86d2c09c33f>", line 1
[*L for L in LOL]
^
SyntaxError: iterable unpacking cannot be used in comprehension
Run Code Online (Sandbox Code Playgroud)
哦.为什么我们不能拥有美好的东西?
理解中的解包似乎很明显/ pythonic,但由于他们不愿意添加该特殊错误消息,因此有理由禁用它.那么,该语法有什么问题?
python list-comprehension python-3.x iterable-unpacking python-3.5
在 Python 中使用[]、使用()或不使用任何方式解包函数调用有什么区别?
def f():
return 0, 1
a, b = f() # 1
[a, b] = f() # 2
(a, b) = f() # 3
Run Code Online (Sandbox Code Playgroud) 我经常使用shift解压缩函数参数:
sub my_sub {
my $self = shift;
my $params = shift;
....
}
Run Code Online (Sandbox Code Playgroud)
然而,我的同事们很多人都在讲道,这shift实际上是邪恶的.你能解释我为什么要这样做吗?
sub my_sub {
my ($self, $params) = @_;
....
}
Run Code Online (Sandbox Code Playgroud)
到shift?
In [55]: a = 5
In [56]: b = 6
In [57]: (a, b) = (b, a)
In [58]: a
Out[58]: 6
In [59]: b
Out[59]: 5
Run Code Online (Sandbox Code Playgroud)
如何交换a和b的值在内部工作?它肯定不使用临时变量.
有没有人知道为什么unary(*)运算符不能用在涉及iterators/lists/tuples的表达式中的原因?
为什么它只限于功能拆包?或者我认为错了?
例如:
>>> [1,2,3, *[4,5,6]]
File "<stdin>", line 1
[1,2,3, *[4,5,6]]
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
为什么不是*运营商:
[1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
而当*操作符与函数调用一起使用时,它会扩展:
f(*[4, 5, 6]) is equivalent to f(4, 5, 6)
Run Code Online (Sandbox Code Playgroud)
使用列表时+和*使用列表时之间存在相似性,但在使用其他类型扩展列表时则不相似.
例如:
# This works
gen = (x for x in range(10))
def hello(*args):
print args
hello(*gen)
# but this does not work
[] + gen
TypeError: can only concatenate …Run Code Online (Sandbox Code Playgroud) python argument-unpacking python-2.7 iterable-unpacking pep448
Python提供了"*"运算符来解压缩元组列表并将它们作为参数提供给函数,如下所示:
args = [3, 6]
range(*args) # call with arguments unpacked from a list
Run Code Online (Sandbox Code Playgroud)
这相当于:
range(3, 6)
Run Code Online (Sandbox Code Playgroud)
有谁知道在PHP中是否有办法实现这一点?一些谷歌搜索"PHP解包"的变化并没有立即出现任何东西..也许它在PHP中被称为不同的东西?
我是python编程的新手,需要你的帮助以下内容:
我想从python中的函数返回两个列表.我怎样才能做到这一点.以及如何在主程序中阅读它们.示例和插图将非常有用.
提前致谢.
python ×9
tuples ×4
python-2.7 ×2
arguments ×1
parameters ×1
pep448 ×1
perl ×1
php ×1
python-3.5 ×1
python-3.x ×1
shift ×1