You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用zip()合并两个列表并去除第二个元素开头的\n

解决方法

你可以在列表推导式里对artistlist的每个元素调用lstrip('\n')(仅去除开头的换行符)或者strip()(去除首尾所有空白字符,包括换行、空格等),同时保留子列表的结构:

cleaned_list = [[song, artist.lstrip('\n')] for song, artist in zip(songlist, artistlist)]

如果歌手名字前后可能还有其他多余空白,用strip()更合适:

cleaned_list = [[song, artist.strip()] for song, artist in zip(songlist, artistlist)]

你之前的列表推导式[i + j for i, j in zip(songlist, artistlist)]会把歌曲名和歌手名直接拼接成一个字符串,所以才不符合需求。上面的写法会保留每个子列表包含歌曲名和处理后歌手名的结构,比如原本的["Amarillo By Morning","\nGeorge Strait"]会变成["Amarillo By Morning","George Strait"]。

内容的提问来源于stack exchange,提问作者anmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:05:42