[英]Multiple if statements list comprehension
Good evening,晚上好,
I'm trying to get better at list comprehension since I discovered this fantastic form of coding a few days ago.自从几天前我发现了这种奇妙的编码形式以来,我正在努力提高列表理解能力。 Currently I'm trying to do a list comprehension with multiple if statements.目前我正在尝试使用多个 if 语句进行列表理解。 This is the code I'm trying to rewrite (for your understanding)这是我正在尝试重写的代码(为了您的理解)
for i in range(len(keys)):
key = keys[i]
if key == 1:
newAns = [1, 0, 0, 0]
answers.append(newAns)
elif key == 2:
newAns = [0, 1, 0, 0]
answers.append(newAns)
elif key == 3:
newAns = [0, 0, 1, 0]
answers.append(newAns)
else:
newAns = [0, 0, 0, 1]
answers.append(newAns)
And this is what i have done so far这就是我到目前为止所做的
answers = [i for i in keys]:
[answers.append([1, 0, 0, 0]) if i == 1]
[answers.append([0, 1, 0, 0]) if i == 2]
[answers.append([0, 0, 1, 0]) if i == 3]
[answers.append([0, 0, 0, 1]) if i == 1]
The list contains values of ints and i would like to convert them to vectors depending on what value the key has.该列表包含整数值,我想根据键的值将它们转换为向量。
I'm a bit stuck and would appreciate some guidance in how to approach this task.我有点卡住了,希望能得到一些关于如何处理这项任务的指导。 Thank you.谢谢你。
How about we put all the key
and newAns
in a dict
and use them in your list comprehension?我们把所有的key
和newAns
放在一个dict
,并在你的列表理解中使用它们怎么样?
answer_map = {1: [1, 0, 0, 0], 2: [0, 1, 0, 0], 3: ...}
answers = [answer_map[x] if x in answer_map else [0, 0, 0, 1] for x in keys]
Update :更新:
I totally forgot about dict.get(key, default)
(Thanks, @U9-Forward!).我完全忘记了dict.get(key, default)
(谢谢,@U9-Forward!)。 You could also say:你也可以说:
[answer_map.get(x, [0, 0, 0, 1]) for x in keys]
Additional to @UltrInstinct's answer, do get
to make no if statements:附加@ UltrInstinct的答案,不要get
if语句做没有:
answer_map = {1: [1, 0, 0, 0], 2: [0, 1, 0, 0], 3: ...}
answers = [answer_map,get(x,[0, 0, 0, 1]) for x in keys]
Now:现在:
print(answers)
Would output the desired output.将输出所需的输出。
As a fan of comprehensions your can use a nested comprehension one liner if作为理解的粉丝,您可以使用嵌套理解单衬,如果
[ [ 1 if i == min(k, 4) else 0 for i in range(1, 5)] for k in keys]
A two-liner with a dictionary带字典的两行
answer_dict = {1: [1, 0, 0, 0],
2: [0, 1, 0, 0] ...}
would be a bit more efficient.效率会高一些。 Just for fun you can construct the dictionary using a nested dictionary comprehension, even though it does not be worth the efort it if you have just four answers.只是为了好玩,您可以使用嵌套的字典理解来构建字典,即使如果您只有四个答案则不值得付出努力。
answer_dict = { k: [ 1 if i == k else 0
for i in range(1, 5) for k in range(1, 5)]
}
[answer_dict[min(4, k)] for k in keys]
Instead of dictionary you can also use a list除了字典,您还可以使用列表
answers = [ [ 1 if i == k else 0
for i in range(1, 5) for k in range(0, 5)]]
[answers[min(4, k)] for k in keys]
You could use nested ternary expressions if you want an absurd one-liner.如果你想要一个荒谬的单行,你可以使用嵌套的三元表达式。
In [4]: keys = [2, 1, 1, 3, 0, 2]
In [5]: result = [
...: [1,0,0,0] if key == 1 else
...: [0,1,0,0] if key == 2 else
...: [0,0,1,0] if key == 3 else
...: [0,0,0,1]
...: for key in keys
...: ]
In [6]: result
Out[6]:
[[0, 1, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0]]
However, it is better to wrap your logic in a function and call that function in the list comprehension:但是,最好将您的逻辑包装在一个函数中并在列表推导式中调用该函数:
In [7]: def f(key):
...: if key == 1:
...: result = [1, 0, 0, 0]
...: elif key == 2:
...: result = [0, 1, 0, 0]
...: elif key == 3:
...: result = [0, 0, 1, 0]
...: else:
...: result = [0, 0, 0, 1]
...: return result
...:
In [8]: [f(key) for key in keys]
Out[8]:
[[0, 1, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0]]
In this case, a dict
works very nicely as well, as demonstrated in other answers.在这种情况下, dict
很好地工作,如其他答案中所示。 In general, don't try to cram a bunch of things into a single list-comprehension.一般来说,不要试图将一堆东西塞进一个列表理解中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.