亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于跨多行分配多個變量的 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 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號