- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 python,我不期望得到答案。我真的需要帮助。
我收到了多个列表,我需要检查这些列表以确保:
a) 这不是一个空列表
b) 列表中有多个整数
c) 通过检查不同的对来检查列表中的整数是否具有偶数乘积值或奇数乘积值。例如list1 = [1,2,3]
。这将返回 False,因为产品是偶数。 list2 = [3,2,3]
将返回 True,因为两个奇数对的乘积是奇数。
以下是我的一些想法:
a) 要检查它是否为空列表,您可以键入:
if not myList:
return(False)
或
if myList != []:
return(True)
b)
if myList != 1:
return(True)
或
if int in myList < 0 and if int in myList > 2:
return(False)
c)
if len(myList) % 2 == 0:
return(False)
因为如果有两个偶数 double ,那么无论哪种方式它都会返回。我只是想找到奇怪的产品。
if len(myList) % 2 != 0:
for i in myList:
if i % i == 1:
return(True)
else:
return(False)
我应该对此进行测试,但实际上我只是想出了写这篇文章的方法。找到配对是相当困难的。
我认为如果最终结果是 1 那么它们是相同的数字 - 对吧?第一次使用这个网站,所以我不熟悉问题的标准协议(protocol)(不过我确实阅读了规则)。
抱歉,如果这很长,感谢所有帮助我的人!
最佳答案
a) It is not an empty list
您的空容器检查看起来不错(只需删除括号)
if not myList:
return False
b) It has more than one integer within the list
这不起作用,这是数字检查而不是列表长度检查:
if myList != 1:
return(True)
您可能想要(这也处理大小写“a”):
if len(myList) < 2:
return False
c) Check if the integers within the list has an even product value or an odd product value by checking for distinct pairs.
这个很棘手,因为您的解释似乎与您的示例不一致:
For example list1 = [1,2,3]. This would return False because the product is even. list2 = [3,2,3] would return True because the product of the two odd pairs is odd.
两个列表的乘积均为偶数,但您为其中一个返回 True
,为另一个返回 False
。
您随后的解释似乎暗示您想要确定奇偶性,而不需要通过注意所有奇数元素=奇数乘积(True)和任何偶数元素=偶数乘积(False)来确定奇偶性,这会导致两个可能的谓词:
def are_all_odd(myList): # odd product True; even product False
return all(element % 2 for element in myList)
def is_any_even(myList): # even product True; odd product False
return any(element % 2 == 0 for element in myList)
但是模运算符%
是除法,它和乘法一样昂贵。为了避免这种情况,我们可以逐位执行此操作:
def are_all_odd(myList): # odd product True; even product False
return all(element & 1 for element in myList)
def is_any_even(myList): # even product True; odd product False
return any(~element & 1 for element in myList)
使用生成器作为 any()
和 all()
的输入意味着一旦确定答案,它们就会停止。但是,数据可能不是最佳顺序,以最大限度地减少测试数量。
关于python - 在 python 中查找不同的对及其乘积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752725/
我希望我的问题有一个非常简单的解决方案。我只是找不到它: 假设您有两个向量(一个是列向量,一个是行向量)A、B: A = [1,2,3] B = [4;5;6] 如果我们按如下方式将它们相乘,我们会得
我有一个 Tuple 的列表: "dog", 25 "cat", 5 "cat", 7 "rat", 4 "dog", 10 我需要的 Linq 查询规则必须满足以下条件:我需要按字符串值对元组进行分
给定 2 个不同的 NDarray,A 和 B,形状相同但尺寸任意,我如何获得 NDarray C,其中 C 是 A 和 B(含)范围内所有整数的乘积。 我的意思是A是起始数组,B是结束数组,我想要数
假设我需要为某些输入构建一个真值表,它要求我提供逻辑和、算术和和逻辑乘积。它们之间有什么区别? 最佳答案 逻辑和 - 一种计算机加法,当一个或两个输入变量为 1 时,结果为 1;当输入变量均为 0 时
我正在尝试执行一个简单的矩阵乘法 vector 乘法,但出于某种原因,我在几次乘法的结果中得到了错误的符号。我不知道为什么会这样,任何指针将不胜感激。 这是我的全部代码,即矩阵 * vector 函数
我在上一个主题中找到了一些关于 cuda 矩阵 vector 积的代码: Matrix-vector multiplication in CUDA: benchmarking & performanc
我遇到的第一个问题是显示三个数字中的最小和最大。出现两个单独的警报 - 第一个警报说第二大数字是最大的(因为它还没有考虑第三个数字),第二个警报正确地指出三个中最大的数字是最大的.不确定为什么会这样—
我有两个矩阵 a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) 我想得到元素乘积,[[1*5,2*6], [3*7,4*8]],等
我有一个数组和一个 vector : ArrayXd m1(3, 1337); ArrayXd v1(1, 1337); ArrayXd result(3, 1337); 现在我想将 m1 的每一行与
我有两个 3D 矩阵: a = np.random.normal(size=[3,2,5]) b = np.random.normal(size=[5,2,3]) 我想要每个切片分别沿 2 轴和 0
我正在创建一个 C++ 软件,我需要一个包装器,它基于 Eigen 库,实现类似于官方网页中解释的运算符* https://eigen.tuxfamily.org/dox/group__Matrixf
我正在尝试将张量 (m, n, o) 分解为矩阵 A(m, r)、B (n, r) 和 C (k, r)。这被称为 PARAFAC 分解。 Tensorly已经做了这种分解。 一个重要的步骤是将 A、
我目前正面临这个问题。我有两个矩阵 MatrixXf答: 0.5 0.5 0.5 0.50.694496 0.548501 0.680067 0.7171110
我有以下 df: df = pd.DataFrame({'A': ['foo', 'bar', 'dex', 'tru'], 'B': ['abc', 'def'
假设我们有 2 个 2X2 numpy 数组: X=np.array([[0,1],[1,0]]) 和 I=np.array([[1,0],[0,1]]) 考虑一下克罗内克产品 XX=X^X 我让符号
我想弄清楚这是 Eigen 中的错误还是我做错了什么。我只想要两个复数 vector [1,i] 和 [1,-i] 的点积。答案是 1*1 + i*(-i) = 2。但是 Eigen 给出的答案是零。
我的 C 代码有问题。我所做的就是这样: #include int main() { float zahlen[2]; for (int i = 0; i < 2; i++) {
为了找到数字的因数,我正在使用函数 void primeFactors(int n) # include # include # include # include using namespa
我是一名优秀的程序员,十分优秀!