英文:
Python List Comprehension: Add n specific elements after each element in a list
问题
Here is the translated code:
# 我想要在列表中的每个元素后面添加n个特定元素,只使用列表推导式。
# 示例:
l = [A, B, C, D, E]
element = ''
# 对于 n = 1,应该得到
l = [A, '', B, '', C, '', D, '', E, '']
# 或者对于 n = 2,应该得到
l = [A, '', '', B, '', '', C, '', '', D, '', '', E, '', '']
Please note that I've only translated the code portion as per your request.
英文:
I would like to add n
specific elements after each element in a list using only list comprehension.
Example:
l = [A, B, C, D, E]
element = ''
for n = 1
should result in
l = [A, '', B, '', C, '', D, '', E, '']
or n = 2
should result in
l = [A, '', '', B, '', '', C, '', '', D, '', '', E, '', '']
答案1
得分: 1
以下是您要求的内容的中文翻译:
这个任务在没有列表理解的情况下相当简单。
如果您想要使用列表理解,可以像这样做:
l = ['A', 'B', 'C', 'D', 'E']
[(x, '') for x in l]
# [('A', ''), ('B', ''), ('C', ''), ('D', ''), ('E', '')]
通过以下方式展开这个嵌套列表:
[item for sublist in ((x, '') for x in l) for item in sublist]
# ['A', '', 'B', '', 'C', '', 'D', '', 'E', '']
同样对于两个空格:
[item for sublist in ((x, '', '') for x in l) for item in sublist]
# ['A', '', '', 'B', '', '', 'C', '', '', 'D', '', '', 'E', '', '']
编辑:
n = 1
[item for sublist in ([x] + [''] * n for x in l) for item in sublist]
# ['A', '', 'B', '', 'C', '', 'D', '', 'E', '']
n = 2
# ['A', '', '', 'B', '', '', 'C', '', '', 'D', '', '', 'E', '', '']
希望这对您有所帮助。
英文:
It is pretty straight-forward without the List Comprehension.
If you want to use List Comprehension you can do something like:
l = ['A','B','C','D','E']
[(x,'') for x in l]
#[('A', ''), ('B', ''), ('C', ''), ('D', ''), ('E', '')]
Flatten this nested list by:
[item for sublist in ((x,'') for x in l) for item in sublist]
#['A', '', 'B', '', 'C', '', 'D', '', 'E', '']
Similarly for two blank spaces:
[item for sublist in ((x,'','') for x in l) for item in sublist]
#['A', '', '', 'B', '', '', 'C', '', '', 'D', '', '', 'E', '', '']
Edit:
n=1
[item for sublist in ([x] + ['']*n for x in l) for item in sublist]
#['A', '', 'B', '', 'C', '', 'D', '', 'E', '']
n=2
#['A', '', '', 'B', '', '', 'C', '', '', 'D', '', '', 'E', '', '']
答案2
得分: 0
只需使用两个循环:
def add_n_elements_after_each(iterable, n, padder):
return [
padder if i else element
for element in iterable
for i in range(0, n + 1)
]
试一试:
print(add_n_elements_after_each(['A', 'B', 'C', 'D'], 2, ''))
# ['A', '', '', 'B', '', '', 'C', '', '', 'D', '', '']
英文:
Simply use two loops:
def add_n_elements_after_each(iterable, n, padder):
return [
padder if i else element
for element in iterable
for i in range(0, n + 1)
]
Try it:
print(add_n_elements_after_each(['A', 'B', 'C', 'D'], 2, ''))
# ['A', '', '', 'B', '', '', 'C', '', '', 'D', '', '']
答案3
得分: 0
也许你应该为列表中的每个项目使用<i>for</i>循环
list_help = []
for l in list:
list_help.append(l)
for _ in range(n):
list_help.append(' ')
英文:
Maybe you should use <i>for</i> loop for every item in list
list_help = []
for l in list:
list_help.append(l)
for _ in range(n):
list_help.append(' ')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论