Append commas between variables
以下是我的代码。我想用逗号分隔的列表附加ip:port字符串。
1 2 3 4 5 6 | ip = ['1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4'] memcache = '' port = '11211' for node in ip: memcache += str(node) + ':' + port # join this by comma but exclude last one |
我想要这种格式的输出:
我怎样才能做到?
1 | memcache = ', '.join(address + ':' + port for address in ip) |
它使用
1 | memcache = ', '.join(address +":" + port for address in ip) |
最好的彼得