如果 list1 == 1,我想遍歷 3 個串列,其中第一個串列是有條件的(1 或 0): print('Some text', item0 in list2 , 'Some text' item0 in list2 , 'some text', item0 in清單 3)
這是我的代碼
dicts = {0:1, 1:1, 2:1}
values = list(dicts.values()) #Makin a list from values in my dict (1 or 0) my new list now contains [1, 1, 1]
cells = ['ABC', 'DEF', 'GHI']
nodes = ['123', '456', '789']
for v, c, n in zip(values, nodes, cells):
if v == 1:
print('Some Text ', n, ' Some Text', n, 'text', c, 'Some Text')
我希望結果是:
Some Text 123 Some Text 123 text ABC Some Text
Some Text 456 Some Text 456 text DEF Some Text
Some Text 789 Some Text 789 text GHI Some Text
我已經嘗試了多種方法,但我似乎無法做對
uj5u.com熱心網友回復:
看起來你剛剛倒置c并n在你的回圈中:
for v, n, c in zip(values, nodes, cells):
if v == 1:
print('Some Text', n, ' Some Text', n, 'text', c, 'Some Text')
注意。如果使用帶有許多引數的列印,則不需要在塊周圍添加空格
輸出:
Some Text 123 Some Text 123 text ABC Some Text
Some Text 456 Some Text 456 text DEF Some Text
Some Text 789 Some Text 789 text GHI Some Text
uj5u.com熱心網友回復:
用這個替換你的列印陳述句
print('Some Text ', c,'Some Text ', c, ' text', n, 'Some Text')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349911.html
