Python列表理解(以if,不带else结束)

时间:2019-01-30 03:11:36

标签: python list if-statement list-comprehension

conditions = [1, 2, 3, 4]
values = [1, 2, 3, 3]

[print("YAY") for c,v in zip(conditions,values) if c==v]

这有效,但是如果我添加else语句,如以下示例所示,则会引发语法错误:

[print("YAY") for c,v in zip(conditions,values) if c==v else print("NAY")]

为什么禁止这样做?如果它起作用了(至少对我来说)会很有意义

2 个答案:

答案 0 :(得分:1)

我同意@kindall的回答,如果您仍要打印,则可以像这样更改代码,这将按您的预期进行:

<?php
session_start();
include "config/koneksi.php";
include "config/library.php";

$nama=trim($_POST['nama_komentar']);
$komentar=trim($_POST['isi_komentar']);

if (empty($nama)){
  echo "Anda belum mengisikan NAMA<br />
  	      <a href=javascript:history.go(-1)><b>Ulangi Lagi</b>";
}
elseif (empty($komentar)){
  echo "Anda belum mengisikan KOMENTAR<br />
  	      <a href=javascript:history.go(-1)><b>Ulangi Lagi</b>";
}
elseif (strlen($_POST['isi_komentar']) > 1000) {
  echo "KOMENTAR Anda kepanjangan, dikurangin atau dibagi jadi beberapa bagian.<br />
  	      <a href=javascript:history.go(-1)><b>Ulangi Lagi</b>";
}
else{
function antiinjection($data){
  $filter_sql = mysql_real_escape_string(stripslashes(strip_tags(htmlspecialchars($data,ENT_QUOTES))));
  return $filter_sql;
}

$nama_komentar = antiinjection($_POST['nama_komentar']);
$url = antiinjection($_POST['url']);
$isi_komentar = antiinjection($_POST['isi_komentar']);


	if(!empty($_POST['kode'])){
		if($_POST['kode']==$_SESSION['captcha_session']){

// Mengatasi input komentar tanpa spasi
$split_text = explode(" ",$isi_komentar);
$split_count = count($split_text);
$max = 57;

for($i = 0; $i <= $split_count; $i++){
if(strlen($split_text[$i]) >= $max){
for($j = 0; $j <= strlen($split_text[$i]); $j++){
$char[$j] = substr($split_text[$i],$j,1);
if(($j % $max == 0) && ($j != 0)){
  $v_text .= $char[$j] . ' ';
}else{
  $v_text .= $char[$j];
}
}
}else{
  $v_text .= " " . $split_text[$i] . " ";
}
}

    $sql = mysql_query("INSERT INTO komentar(nama_komentar,url,isi_komentar,id_berita,tgl,jam_komentar) 
                        VALUES('$nama_komentar','$url','$v_text','$_POST[id]','$tgl_sekarang','$jam_sekarang')");

    echo "<meta http-equiv='refresh' content='0; url=berita-$_POST[id].html'>";
		}else{
			echo "Kode yang Anda masukkan tidak cocok<br />
			      <a href=javascript:history.go(-1)><b>Ulangi Lagi</b></a>";
		}
	}else{
		echo "Anda belum memasukkan kode<br />
  	      <a href=javascript:history.go(-1)><b>Ulangi Lagi</b></a>";
	}
}
?>

答案 1 :(得分:0)

对于单数if,语法为:

[print("YAY") for c,v in zip(conditions,values) if c==v]

这意味着:

for c,v in zip(conditions,values):
    if c==v:
        print('YAY')

对于多个ifelifelse,语法为:

[print("YAY") if c==v else print("lalala") if c=='some other condition' else print("NAY") for c,v in zip(conditions,values)]

这翻译成

for c,v in zip(conditions,values):
    if c==v:
        print('YAY')
    elif c=='some other condition':
        print('lalala')
    else:
        print('NAY')

我们可以对此进行测试:

conditions = [1, 2, 3, 4, 'some other condition']
values = [1, 2, 3, 3, 999]
[print("YAY") if c==v else print("lalala") if c=='some other condition' else print("NAY") for c,v in zip(conditions,values)]
#YAY
#YAY
#YAY
#NAY
#lalala

编辑:如果要处理列表推导中的嵌套for循环,请注意以下代码是等效的:

newlist = []
for c in conditions:
    for v in values:
        newlist.append((c,v))
print (newlist)
# [(1, 1), (1, 2), (1, 3), (1, 3), (1, 999), (2, 1), (2, 2), (2, 3), (2, 3), (2, 999), (3, 1), (3, 2), (3, 3), (3, 3), (3, 999), (4, 1), (4, 2), (4, 3), (4, 3), (4, 999), ('some other condition', 1), ('some other condition', 2), ('some other condition', 3), ('some other condition', 3), ('some other condition', 999)]

newlist = [(c,v) for c in conditions for v in values]
print (newlist)
#[(1, 1), (1, 2), (1, 3), (1, 3), (1, 999), (2, 1), (2, 2), (2, 3), (2, 3), (2, 999), (3, 1), (3, 2), (3, 3), (3, 3), (3, 999), (4, 1), (4, 2), (4, 3), (4, 3), (4, 999), ('some other condition', 1), ('some other condition', 2), ('some other condition', 3), ('some other condition', 3), ('some other condition', 999)]

请注意,两个代码段的for c in conditions是外部循环,for v in values是内部循环