Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我的清单:
animals = ['dog', 'cat', 'mouse']
我想拆分它,所以我最终得到一个包含列表中三个字符串的字符串,如下所示:
dog/cat/mouse
我尝试使用以下代码,但它只打印原始列表:
print [e.split('/')[0] for e in animals]
哪里不对了?
你不想split你想join,莫名其妙地逆向操作。
split
join
animals = ['dog', 'cat', 'mouse'] "/".join(animals)
加入效果很好。
如果你有一个固定的字符串,你想用固定数量的列表元素填充,你也可以这样做:
>>> '{}+{}-{}'.format(*(t for t in animals)) 'dog+cat-mouse'