我有一些像这样的Perl代码:
my $match = $matches{$key}
? "$matches{$key} was a match!"
: "There was no match."
如何在Python中最好地重写?我试图避免得到KeyError。
答案 0 :(得分:3)
此。
message = "%s was a match"%(matches[key],) if key in matches else "There was no match."