要理解为什么我在程序中遇到错误,我试图找到一个决定因素的"次要",我写了一个更简单的程序,因为我的变量搞砸了.下面这个函数接受一个2*2矩阵作为输入,并返回一个包含其行的列表(毫无意义和低效,我知道,但我正在试图理解这背后的理论).
def alpha(A): #where A will be a 2 * 2 matrix
B = A #the only purpose of B is to store the initial value of A, to retrieve it later
mylist = []
for i in range(2):
for j in range(2):
del A[i][j]
array.append(A)
A = B
return mylist
Run Code Online (Sandbox Code Playgroud)
但是,在这里似乎B被动态赋值为A ,因为我无法将B的初始值存储在B中以便以后使用它.这是为什么?
假设我想检查给定元组中是否存在两个给定元素中的任何一个,例如:
if foo in my_tuple or bar in my_tuple:
Run Code Online (Sandbox Code Playgroud)
是否有更多的pythonic方式来构建这个表达式?具体来说,如果我想检查几个元素,那么这些语句会变得烦人.我试过了
if (foo or bar) in my_tuple:
Run Code Online (Sandbox Code Playgroud)
但这会选择foo over bar并仅检查foo.将不胜感激.