标签: list-comprehension

python list comprehension在一次迭代中生成两个值

我想在python中生成一个列表如下 -

[1, 1, 2, 4, 3, 9, 4, 16, 5, 25 .....]
Run Code Online (Sandbox Code Playgroud)

你会想通的,它只不过是 n, n*n

我尝试在python中编写这样的列表理解如下 -

lst_gen = [i, i*i for i in range(1, 10)]
Run Code Online (Sandbox Code Playgroud)

但这样做会产生语法错误.

通过列表理解生成上面列表的好方法是什么?

python list-comprehension list

42
推荐指数
6
解决办法
3万
查看次数

展平NumPy数组列表?

看来我的数据格式为NumPy数组列表(type() = np.ndarray):

[array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), 
array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), 
array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]), 
array([[ 0.00353654]]), array([[ 0.00353654]]), array([[ 0.00353654]]),
array([[ 0.00353654]])]
Run Code Online (Sandbox Code Playgroud)

我想把它变成一个polyfit函数:

m1 = np.polyfit(x, y, deg=2)
Run Code Online (Sandbox Code Playgroud)

但是,它返回错误: TypeError: expected 1D vector for x

我假设我需要将我的数据展平为:

[0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654, 0.00353654 ...]
Run Code Online (Sandbox Code Playgroud)

我已经尝试了列表理解,它通常适用于列表列表,但是这正如预期的那样无效:

[val for sublist in risks for val in sublist]
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

python arrays numpy list-comprehension

42
推荐指数
4
解决办法
3万
查看次数

如何在列表推导中使用重新匹配对象

我有一个函数从字符串列表中挑出块并将它们作为另一个列表返回:

def filterPick(lines,regex):
    result = []
    for l in lines:
        match = re.search(regex,l)
        if match:
            result += [match.group(1)]
    return result
Run Code Online (Sandbox Code Playgroud)

有没有办法将其重新表述为列表理解?显然它是相当清楚的; 只是好奇.


感谢那些贡献的人,特别提到了@Alex.这是我最终得到的浓缩版本; 正则表达式匹配方法作为"预先提升"参数传递给filterPick:

import re

def filterPick(list,filter):
    return [ ( l, m.group(1) ) for l in list for m in (filter(l),) if m]

theList = ["foo", "bar", "baz", "qurx", "bother"]
searchRegex = re.compile('(a|r$)').search
x = filterPick(theList,searchRegex)

>> [('bar', 'a'), ('baz', 'a'), ('bother', 'r')]
Run Code Online (Sandbox Code Playgroud)

python regex list-comprehension

41
推荐指数
4
解决办法
7万
查看次数

可以从列表理解中返回两个列表吗?

是否可以从列表理解中返回两个列表?嗯,这显然不起作用,但是类似于:

rr, tt = [i*10, i*12 for i in xrange(4)]
Run Code Online (Sandbox Code Playgroud)

所以rrtt都是从搜索结果列表i*10i*12分别.非常感谢

python list-comprehension

39
推荐指数
2
解决办法
1万
查看次数

如何使用列表理解获得两个列表的并集?

请考虑以下列表:

a = ['Orange and Banana', 'Orange Banana']
b = ['Grapes', 'Orange Banana']
Run Code Online (Sandbox Code Playgroud)

如何获得以下结果:

c = ['Orange and Banana', 'Orange Banana', 'Grapes']
Run Code Online (Sandbox Code Playgroud)

python list-comprehension

39
推荐指数
2
解决办法
7万
查看次数

如何限制理解的大小?

我有一个list想要建立(通过理解)另一个列表.我想通过条件限制这个新列表的大小

以下代码将失败:

a = [1, 2, 1, 2, 1, 2]
b = [i for i in a if i == 1 and len(b) < 3]
Run Code Online (Sandbox Code Playgroud)

Traceback (most recent call last):
  File "compr.py", line 2, in <module>
    b = [i for i in a if i == 1 and len(b) < 3]
  File "compr.py", line 2, in <listcomp>
    b = [i for i in a if i == 1 and len(b) < 3]
NameError: name 'b' is not …
Run Code Online (Sandbox Code Playgroud)

python list-comprehension

39
推荐指数
2
解决办法
3971
查看次数

具有重复值和后缀的列表

我有一个清单,a:

a = ['a','b','c']
Run Code Online (Sandbox Code Playgroud)

并且需要复制一些带有以_ind这种方式添加的后缀的值(顺序很重要):

['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
Run Code Online (Sandbox Code Playgroud)

我试过了:

b = [[x, x + '_ind'] for x in a]
c = [item for sublist in b for item in sublist]
print (c)
['a', 'a_ind', 'b', 'b_ind', 'c', 'c_ind']
Run Code Online (Sandbox Code Playgroud)

我认为我的解决方案有点过于复杂.有没有更好,更pythonic的解决方案?

python list-comprehension list suffix

39
推荐指数
5
解决办法
1737
查看次数

如何在List的每个元素上调用方法?

假设我有一个汽车列表:

public class Car {
    private String brand;
    private String name;
    private String color;

    public Car() { // ...  }

    public getName() { return name; }
    // ...
}
Run Code Online (Sandbox Code Playgroud)
// Suppose that I have already init the list of car
List<Car> cars = //...
List<String> names = new ArrayList<String>();


for (Car c : cars ) {
    names.add(c.getName());
}
Run Code Online (Sandbox Code Playgroud)

如何缩短上面的代码?简而言之,如何在List的每个元素上调用方法?

例如,在Python中:

[car.name for car in cars]
Run Code Online (Sandbox Code Playgroud)

java list-comprehension list

38
推荐指数
2
解决办法
6万
查看次数

具有多个'if'的python列表理解

我们都知道python的

[f(x) for x in y if g(x)]
Run Code Online (Sandbox Code Playgroud)

句法.

但是 ,列表推导的AST表示有多个'if'表达式的空间:

comprehension = (expr target, expr iter, expr* ifs)
Run Code Online (Sandbox Code Playgroud)

有人能给我一个python代码的例子,它会产生一个带有多个'if'表达式的AST吗?

python if-statement list-comprehension

37
推荐指数
4
解决办法
4万
查看次数

嵌套列表理解有两个列表

我理解简单列表理解是如何工作的,例如:

[x*2 for x in range(5)] # returns [0,2,4,6,8]
Run Code Online (Sandbox Code Playgroud)

而且我也理解嵌套列表comprehesion的工作原理:

w_list = ["i_have_a_doubt", "with_the","nested_lists_comprehensions"]

# returns the list of strings without underscore and capitalized
print [replaced.title() for replaced in [el.replace("_"," ")for el in w_list]]
Run Code Online (Sandbox Code Playgroud)

所以,当我尝试这样做的时候

l1 = [100,200,300]
l2 = [0,1,2]
[x + y for x in l2 for y in l1 ]
Run Code Online (Sandbox Code Playgroud)

我期待这个:

[101,202,303]
Run Code Online (Sandbox Code Playgroud)

但我得到了这个:

[100,200,300,101,201,301,102,202,302]
Run Code Online (Sandbox Code Playgroud)

所以我有一个更好的方法解决问题,这给了我想要的东西

[x + y for x,y in zip(l1,l2)]
Run Code Online (Sandbox Code Playgroud)

但我不理解第一个代码上9个元素的返回

python nested list-comprehension

37
推荐指数
3
解决办法
5万
查看次数

标签 统计

list-comprehension ×10

python ×9

list ×3

arrays ×1

if-statement ×1

java ×1

nested ×1

numpy ×1

regex ×1

suffix ×1