Python列表推导:在列表中的每个元素后面添加n个特定元素

huangapple go评论66阅读模式
英文:

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(&#39; &#39;)

huangapple
  • 本文由 发表于 2023年5月25日 18:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定