我有一个生成器(数字)和一个值(数字).我想迭代这些,好像它们是一个序列:
i for i in tuple(my_generator) + (my_value,)
Run Code Online (Sandbox Code Playgroud)
问题是,据我所知,这会创建3个元组,只是立即丢弃它们,并且还会复制"my_generator"中的项目一次.
更好的approch将是:
def con(seq, item):
for i in seq:
yield seq
yield item
i for i in con(my_generator, my_value)
Run Code Online (Sandbox Code Playgroud)
但我想知道没有那个函数定义是否可以做到这一点
我可以调用函数列表并使用列表理解吗?
def func1():return 1
def func2():return 2
def func3():return 3
fl = [func1,func2,func3]
fl[0]()
fl[1]()
fl[2]()
Run Code Online (Sandbox Code Playgroud)
我知道我能做到
for f in fl:
f()
Run Code Online (Sandbox Code Playgroud)
但我可以在下面做吗?
[f() for f in fl]
Run Code Online (Sandbox Code Playgroud)
例如,如果我的职能列表在课堂上,那么对于那些善良的人来说是另一个问题
class F:
def __init__(self):
self.a, self.b, self.c = 0,0,0
def func1(self):
self.a += 1
def func2(self):
self.b += 1
def func3(self):
self.c += 1
fl = [func1,func2,func3]
fobj= F()
for f in fobj.fl:
f()
Run Code Online (Sandbox Code Playgroud)
它有用吗?
我正在尝试在Python中创建矩阵转置函数.矩阵是二维数组,表示为整数列表.例如,以下是2X3矩阵(意味着矩阵的高度为2,宽度为3):
A=[[1, 2, 3],
[4, 5, 6]]
Run Code Online (Sandbox Code Playgroud)
要转换第i个索引中的第j个项应该成为第j个索引中的第i个项.以下是上述示例的转置方式:
>>> transpose([[1, 2, 3],
[4, 5, 6]])
[[1, 4],
[2, 5],
[3, 6]]
>>> transpose([[1, 2],
[3, 4]])
[[1, 3],
[2, 4]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在尝试编写一个列表理解语句,如果它当前未包含在列表中,则只会添加一个项目.有没有办法检查当前正在构建的列表中的当前项?这是一个简短的例子:
输入
{
"Stefan" : ["running", "engineering", "dancing"],
"Bob" : ["dancing", "art", "theatre"],
"Julia" : ["running", "music", "art"]
}
Run Code Online (Sandbox Code Playgroud)
产量
["running", "engineering", "dancing", "art", "theatre", "music"]
Run Code Online (Sandbox Code Playgroud)
代码不使用列表理解
output = []
for name, hobbies in input.items():
for hobby in hobbies:
if hobby not in output:
output.append(hobby)
Run Code Online (Sandbox Code Playgroud)
我的尝试
[hobby for name, hobbies in input.items() for hobby in hobbies if hobby not in ???]
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中有一个很好的方法可以做到这一点,但我对这门语言很新,所以请原谅我,如果这很容易!
我有一个列表,我想从该列表中挑选出某些值.我想要选择的值是列表中的索引在另一个列表中指定的值.
例如:
indexes = [2, 4, 5]
main_list = [0, 1, 9, 3, 2, 6, 1, 9, 8]
Run Code Online (Sandbox Code Playgroud)
输出将是:
[9, 2, 6]
Run Code Online (Sandbox Code Playgroud)
(即main_list中索引为2,4和5的元素).
我觉得这应该是可行的,使用像列表推导这样的东西,但我无法弄清楚(特别是,我无法弄清楚如何使用列表理解时访问项目的索引).
我有一个与此类似的元组列表:
l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]
Run Code Online (Sandbox Code Playgroud)
我想创建一个简单的单行程序,它会给我以下结果:
r = (25, 20) or r = [25, 20] # don't care if tuple or list.
Run Code Online (Sandbox Code Playgroud)
这就像做以下事情:
r = [0, 0]
for t in l:
r[0]+=t[0]
r[1]+=t[1]
Run Code Online (Sandbox Code Playgroud)
我确信这很简单,但我想不出来.
注意:我已经查看了类似的问题:
当我需要在列表中添加几个相同的项目时,我使用list.extend:
a = ['a', 'b', 'c']
a.extend(['d']*3)
Run Code Online (Sandbox Code Playgroud)
结果
['a', 'b', 'c', 'd', 'd', 'd']
Run Code Online (Sandbox Code Playgroud)
但是,如何与列表理解类似?
a = [['a',2], ['b',2], ['c',1]]
[[x[0]]*x[1] for x in a]
Run Code Online (Sandbox Code Playgroud)
结果
[['a', 'a'], ['b', 'b'], ['c']]
Run Code Online (Sandbox Code Playgroud)
但我需要这个
['a', 'a', 'b', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我使用了很多N维数组,并且编写这样的缩进代码会很麻烦,我知道一些代码可以用列表推导和内联语句替换.例如:
for x in (0,1,2,3):
for y in (0,1,2,3):
if x < y:
print (x, y, x*y)
Run Code Online (Sandbox Code Playgroud)
可以替换为:
print [(x, y, x * y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]
Run Code Online (Sandbox Code Playgroud)
但是我怎么能改变动作而不是打印来做其他事情:
total = x+y
Run Code Online (Sandbox Code Playgroud)
所以我想做的是:
[(total+=x+y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]
Run Code Online (Sandbox Code Playgroud)
但这不起作用
是否有一种聪明的方法来做到这一点,而不是:
for x in (0,1,2,3):
for y in (0,1,2,3):
if x < y:
total+=x+y
Run Code Online (Sandbox Code Playgroud) 在调试我的代码时,我想使用列表理解.但是,当我在函数内部时,似乎无法从调试器中评估列表理解.
我使用的是Python 3.4.
脚本内容:
$ cat test.py
#!/usr/bin/python
def foo():
x = [1, 2, 3, 3, 4]
print(x)
foo()
Run Code Online (Sandbox Code Playgroud)
交互式调试:
$ python3 -mpdb test.py
> /tmp/test.py(3)<module>()
-> def foo():
(Pdb) step
> /tmp/test.py(8)<module>()
-> foo()
(Pdb)
--Call--
> /tmp/test.py(3)foo()
-> def foo():
(Pdb)
> /tmp/test.py(4)foo()
-> x = [1, 2, 3, 3, 4]
(Pdb)
> /tmp/test.py(6)foo()
-> print(x)
(Pdb) p [x for _ in range(1)]
*** NameError: name 'x' is not defined
(Pdb) p x
[1, 2, 3, 3, …Run Code Online (Sandbox Code Playgroud) python ×10
list ×6
python-3.x ×2
debugging ×1
dictionary ×1
function ×1
generator ×1
inline ×1
iterator ×1
performance ×1
python-2.7 ×1
python-3.5 ×1