python - 坚持使用 Python if-else 一行语句
问题描述
在进行赋值操作时,我遇到了嵌套单行条件语句的问题。
bob = list(map(int, input().split()))
print(alice, bob)
a, b = 0
for i in range(3):
a+=1 if alice[i]>bob[i] else (b+=1 if alice[i]<bob[i] else 1)
print(a,b)```
Output: <br/>
File "/home/Algorithms/compare_triplets.py", line 6
a+=1 if alice[i]>bob[i] else b=b+1 if alice[i]<bob[i] else 1
解决方案
您提供的代码有点不清楚,也没有解释明确的目标。这是基于我的假设实现接近目标的代码:
bob = list(map(int, input().split()))
print(alice, bob)
a, b = 0
for i in range(3):
if alice[i]>bob[i]:
a+=1
else:
b+=1
print(a,b)
推荐阅读
- python-3.x - 正则表达式匹配 Dataframe Python 中的列项
- python - 检索主键为元组 (a,b) 且所有行 (a,b') 必须存在于 b' 值的给定列表的 a 列值
- javascript - 如何存储套接字 id 并发送给该用户
- r - 如何根据日期和值从数据框中选择行?
- reactjs - ReactNative:如何让第二个文本组件共享 1 个文本组件溢出的行
- c - 堆栈显示垃圾值
- python-3.x - 在日历上循环三遍
- wear-os - 应该使用什么来代替已弃用的 RotaryEncoder.getScaledScrollFactor() 方法?
- java - 有没有一种优雅的方法来重命名 Java 布尔值以提高我的代码的可读性?
- python - How to Cancel Subscription created think Checkout Session Stripe API