以下是示例:
age = 10
reject = False
if age < 10:
st = 'Kid'
reject = True
else:
st='Adult'
reject = False
有可能吗?类似的东西:
statement1:statement2 if age < 10 else statement3:statment4
我仍然在理解Python中的三元运算符时遇到问题。
答案 0 :(得分:5)
作业声明支持multiple targets:
>>> age = 10
>>> st, reject = ('Kid', True) if age < 10 else ('Adult', False)
>>> st, reject
('Adult', False)
答案 1 :(得分:1)
你可以这样做:
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term and then remove all hyphens, spaces and apostrophes
$searchTerm = $_GET['term'];
$searchTerm = str_replace('-', '', $searchTerm);
$searchTerm = str_replace(' ', '', $searchTerm);
$searchTerm = str_replace("'", "", $searchTerm);
$query = $db->query("SELECT myfield FROM mytable
WHERE replace(replace(replace(myfield,'-',''),'\'',''),' ','') LIKE '%$searchTerm%'
");
while ($row = $query->fetch_assoc()) {
$data[] = $row['myfield'];
}
//return json data
echo json_encode($data);
答案 2 :(得分:0)
您可以使用:
st, reject = ('Kid', True) if age < 10 else ('Adult', False)
使用时:
var1, var2 = 1, 2
您的行为与:
相同var1 = 1
var2 = 2
当你使用:
var1 = 1 if x == y else 2
你的行为与:
相同if x == y:
var1 = 1
else:
var1 = 2
如果你想使用这个定义倍数变量,你必须用变量创建一个元组()
,它们将被解压缩。