英文:
How can i zip() two lists together without the output having "\n" at the beginning of every second element?
问题
I currently have two lists
songlist = []
artistlist = []
I return
list(zip(songlist, artistlist))
and it gives me what I want (a 3d list of a 2d list containing each song title and respective artist) but before each second item in each list is "\n."
For example, a list in the 3d list would look like: ["Amarillo By Morning", "\nGeorge Strait"]
I'm gonna be using the artist in a later function, so I'd rather there not be the "\n" there.
Instead of my above code, I tried returning a variable containing:
[i + j for i, j in zip(songlist, artistlist)]
but that just returned: ["Amarillo By Morning\nGeorge Strait", ... cont`.
英文:
I currently have two lists
songlist = []
artistlist = []
I return
list(zip(songlist, artistlist))
and it gives me what I want(a 3d list of a 2d list containing each song title and respective artist) but before each second item in each list is "\n"
for example a list in the 3d list would look like: ["Amarillo By Morning","\nGeorge Strait"]
I'm gonna be using the artist in a later function so I'd rather there not be the \n there.
instead of my above code i tried returning a variable containing:
[i + j for i, j in zip(songlist, artistlist)]
but that just returned: ["Amarillo By Morning\nGeorge Strait", ... cont
.
答案1
得分: 1
你可以在每次遇到 artist
时对其进行修改,而不必创建另一个迭代器,如果无法在源头修改它。
如果你想要一个列表,而不是使用字符串连接,也可以返回一个列表。
[[song, artist.strip()] for song, artist in zip(songlist, artistlist)]
英文:
To avoid making another iterator, you can just modify each artist
as they come along, if you can't modify it at the source.
Also, return a list instead of using string concatenation if you want a list.
[[song, artist.strip()] for song, artist in zip(songlist, artistlist)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论