If A[0]!=B[0] and A[1]!=B[1] and A[2]!=B[2]:print(数值都不一样) elif A[0]==B[0] and A[1]==B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ elif A[0]!=B[0] and A[1]!=B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ elif A[0]!=B[0] and A[1]==B[1] and A[2]!=B[2]: update table set name = ‘数值存在一样’ elif A[0]==B[0] and A[1]!=B[1] and A[2]!=B[2]: update table set name = ‘数值存在一样’ elif A[0]==B[0] and A[1]==B[1] and A[2]!=B[2]: update table set name = ‘数值存在一样’ elif A[0]==B[0] and A[1]!=B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ elif A[0]!=B[0] and A[1]==B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ else (数值存在一样)有其他更好的写法吗?
时间: 2023-09-25 18:11:10 浏览: 26
可以使用set()函数和len()函数来简化代码,如下所示:
```
if len(set(A) & set(B)) == 0:
print('数值都不一样')
elif len(set(A) & set(B)) == 3:
update table set name = '数值存在一样'
else:
update table set name = '数值存在一样'
```
其中,set(A) & set(B)可以用来找到A和B中相同的元素,len()函数用来计算相同元素的个数。如果没有相同元素,则输出“数值都不一样”,如果有三个相同元素,则更新表格为“数值存在一样”,否则也更新表格为“数值存在一样”。
相关问题
Python if a=1 and b != 2 or c=3
这段代码有语法错误,应该修改为:
```python
if a == 1 and b != 2 or c == 3:
# do something
```
注意到:
- `==` 是判断相等的符号,`=` 是赋值符号;
- `and` 和 `or` 是逻辑运算符,分别表示逻辑与和逻辑或;
- 应该将 `a == 1` 和 `b != 2` 用括号括起来,以确保它们被先执行,再执行 `or` 运算。因为 `and` 运算的优先级高于 `or` 运算。
If A[0]!=B[0] and A[1]!=B[1] and A[2]!=B[2]:print(数值都不一样) elif A[0]==B[0] and A[1]==B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ elif A[0]!=B[0] and A[1]!=B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ elif A[0]!=B[0] and A[1]==B[1] and A[2]!=B[2]: update table set name = ‘数值存在一样’ elif A[0]==B[0] and A[1]!=B[1] and A[2]!=B[2]: update table set name = ‘数值存在一样’ elif A[0]==B[0] and A[1]==B[1] and A[2]!=B[2]: update table set name = ‘数值存在一样’ elif A[0]==B[0] and A[1]!=B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ elif A[0]!=B[0] and A[1]==B[1] and A[2]==B[2]: update table set name = ‘数值存在一样’ else (数值存在一样)请完整写出上面这段代码的高级写法
这段代码可以使用集合(set)来简化,并使用条件表达式进行简化:
```
if set(A) != set(B):
print('数值都不一样')
else:
update_name = '数值存在一样' if A[0]==B[0] and A[1]==B[1] and A[2]==B[2] else '数值存在一样'
update table set name = update_name
```
这段代码首先将 A 和 B 转换成集合(set)类型,比较两个集合是否相同。如果集合不同,则输出"数值都不一样";否则使用条件表达式判断 A 和 B 中数值是否存在相同的情况,并将结果赋值给 update_name 变量。最后根据 update_name 更新数据库。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)