使用三元操作for循環當我在Python 3測試三元運算符,我碰到這種怪異的現象怪異的行爲,而在Python 3.5
import string
test = ""
testing = chr(0) if chr(0) in test else ""
for c in string.ascii_letters + testing:
print(c)
來將打印每行A〜Z 1個字符,但
import string
test = ""
for c in string.ascii_letters + chr(0) if chr(0) in test else "":
print(c)
不會打印任何東西。
有人可以給我一個解釋嗎?
在string.ascii_letters +中更改爲'for c +(chr(0)if chr(0)in test else「」)'並且它將起作用。 – Maroun
有沒有Python字符串包含NUL字符的情況或平臺? – Evert
檢查運算符優先順序:https://docs.python.org/3/reference/expressions.html#operator-precedence – falsetru