鍍金池/ 問答/Python/ python 輸出問題

python 輸出問題

代碼:

ports = [80, 443]
value_port = {'TX_80': 2, 'RX_443': 3, 'RX_80': 1, 'TX_443': 10}
print value_port
for port in ports:
    for key, value in value_port.items():
        if key.endswith(str(port)):
            print key+'='+str(value),

輸出的是
RX_80=1 TX_80=2 RX_443=3 TX_443=10

請教一下 怎么根據(jù)端口不同輸出像下面那樣不同的行
RX_80=1 TX_80=2
RX_443=3 TX_443=10

回答
編輯回答
淡墨
ports = [80, 443]
value_port = {'TX_80': 2, 'RX_443': 3, 'RX_80': 1, 'TX_443': 10}
res_str= ''
for port in ports:
    for key, value in value_port.items():
        if key.endswith(str(port)):
            res_str += key+'='+str(value) + ' '
    res_str += '\r\n'

print(res_str)
2017年12月31日 01:51