def add_peer_function():
all_devices=[item for item in "cisco,linux".split(',')] # or `all_devices = ["cisco", "linux"]`
print(', '.join(all_devices)) # A prettier way to print list Thanks to Philo
add_peer_function()
2:
def add_peer_function():
all_devices=[]
for item in "cisco,linux".split(','): # or `all_devices = ["cisco", "linux"]`
all_devices.append(item)
print(', '.join(all_devices)) # A prettier way to print list Thanks to Philo
add_peer_function()
当然,如果您将字符串
'cisco,linux'
附加到一个列表中,您将得到['cisco,linux']
,它是这个列表在Python中的字符串表示形式。你知道吗你知道什么是split字符串。你知道吗
尝试:
默认情况下,Python按其约定打印对象:字符串位于引号之间。你知道吗
如果您想得到另一种格式,您可以编写自己的格式化程序。你知道吗
对于字符串列表,Python中的一个常见模式是:
最终用另一个分隔符替换''。你知道吗
最后,请注意,“cisco,linux”只是一个带有coma的字符串,它不同于字符串列表:[“cisco”,“linux”]。你知道吗
嗯,我知道两种可能的方法,但第一种更快:
1:
2:
Pythonstr.split文档。你知道吗
Pythonstr.join文档。你知道吗
Pythonlist comprehension文档。你知道吗
相关问题 更多 >
编程相关推荐