Python 遍历数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33446708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating through array
提问by Swift
I have an array of bools and now I want to swap those entries for numbers.
我有一组布尔值,现在我想将这些条目交换为数字。
False => 0
True => 1
I have written two different pieces of code and I would like to know, which one is better and why. This is not so much about actually solving the problem, as about learning.
我写了两段不同的代码,我想知道哪一段更好,为什么。这与其说是关于实际解决问题,不如说是关于学习。
arr = [[True,False],[False,True],[True,True]]
for i,row in enumerate(arr):
for j,entry in enumerate(row):
if entry:
arr[i][j] = 1
else:
arr[i][j] = 0
print(arr)
And the second approach:
第二种方法:
arr = [[True,False],[False,True],[True,True]]
for i in range(len(arr)):
for j in range(len(arr[i])):
if arr[i][j]:
arr[i][j] = 1
else:
arr[i][j] = 0
print(arr)
I read that there are ways to do this with importing itertools
or similar. I am really not a fan of importing things if it can be done with “on-board tools”, but should I rather be using them for this problem?
我读到有一些方法可以通过导入itertools
或类似方法来做到这一点。如果可以使用“板载工具”完成,我真的不喜欢导入东西,但我应该使用它们来解决这个问题吗?
采纳答案by John1024
Let's define your array:
让我们定义你的数组:
>>> arr = [[True,False],[False,True],[True,True]]
Now, let's convert the booleans to integer:
现在,让我们将布尔值转换为整数:
>>> [[int(i) for i in row] for row in arr]
[[1, 0], [0, 1], [1, 1]]
Alternatively, if we want to be more flexible about what gets substituted in, we can use a ternary statement:
或者,如果我们想要更灵活地替换什么,我们可以使用三元语句:
>>> [[1 if i else 0 for i in row] for row in arr]
[[1, 0], [0, 1], [1, 1]]
回答by thebjorn
If you want to stay with a for-loop (e.g. because you want to mutate the existing array instead of creating a new one), you should simplify the code.
如果您想继续使用 for 循环(例如,因为您想改变现有数组而不是创建新数组),您应该简化代码。
I would first simplify the outer loop by removing the indexing (there is no need for it since it's even easier to modify a row than a nested array):
我将首先通过删除索引来简化外部循环(不需要它,因为修改行比修改嵌套数组更容易):
for row in arr:
for j, entry in enumerate(row):
if entry:
row[j] = 1
else:
row[j] = 0
these kinds of simple if statement can often be simplified by using an if expression:
这些简单的 if 语句通常可以通过使用 if 表达式来简化:
row[j] = 1 if entry else 0
but in this case we can do even better. bool
is a subclass of int
(ie. all bool
's are int
's), and True
and False
are defined to be exactly 1
and 0
respectively -- if you scroll down to the specification section of PEP 285 (https://www.python.org/dev/peps/pep-0285/) you'll see that that equivalence is not accidental but very much by design.
但在这种情况下,我们可以做得更好。bool
是int
(即所有bool
的都是int
)的子类,并且True
和False
被定义为完全1
和0
分别——如果你向下滚动到 PEP 285 的规范部分(https://www.python.org/dev/ peps/pep-0285/)你会看到这种等价性不是偶然的,而是设计的。
We can therefore use the int
constructor to grab the underlying integer values[*], since int(True) == 1
and int(False) == 0
, the if-expression can be simplified to:
因此,我们可以使用int
构造函数来获取底层整数值 [*],因为int(True) == 1
和int(False) == 0
,if 表达式可以简化为:
row[j] = int(entry)
[*] technically this is an explicit upcast to a base class, and not a conversion constructor..
[*] 从技术上讲,这是对基类的显式向上转换,而不是转换构造函数..
The simplified code:
简化代码:
for row in arr:
for j, entry in enumerate(row):
row[j] = int(entry)