为了账号安全,请及时绑定邮箱和手机立即绑定

用于跨多行分配多个变量的 Python 语法

用于跨多行分配多个变量的 Python 语法

蝴蝶刀刀 2022-10-06 19:44:14
我试图在一个语句中为多个变量赋值,但我不知道是否有一种很好的语法可以将它拆分为多行。# This is the long version I don't wanta, b, c, d = tf.constant(1, name='constant_a'), tf.constant(2, name='constant_b'), tf.constant(3, name='constant_c'), tf.constant(4, name='constant_d')# Does something like this exist?a, b, c, d = tf.constant(1, name='constant_a'), /             tf.constant(2, name='constant_b'), /             tf.constant(3, name='constant_c'), /             tf.constant(4, name='constant_d')有没有一种不错的 Pythonic 方式来做到这一点?
查看完整描述

3 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

我认为您正在考虑反斜杠(续行)。

a, b, c, d = tf.constant(1, name='constant_a'), \
             tf.constant(2, name='constant_b'), \
             tf.constant(3, name='constant_c'), \
             tf.constant(4, name='constant_d')

这有效,但它很丑。最好使用Joseph 的回答中的元组/列表,或者更好的是Josh 的回答中的理解。


查看完整回答
反对 回复 2022-10-06
?
aluckdog

TA贡献1847条经验 获得超7个赞

不确定它是否更具可读性/Pythonic,但这是最简洁的!

a, b, c, d = [tf.constant(i, name='constant_' + x) for i, x in zip(range(1, 5), 'abcd')]



查看完整回答
反对 回复 2022-10-06
?
largeQ

TA贡献2039条经验 获得超8个赞

把你有的东西放在括号里。

a, b, c, d = (tf.constant(1, name='constant_a'), 
              tf.constant(2, name='constant_b'), 
              tf.constant(3, name='constant_c'),
              tf.constant(4, name='constant_d'))


查看完整回答
反对 回复 2022-10-06
  • 3 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号