在Python3中在循环中向列表追加元素(再次)

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

Appending to lists within loops in Python3 (again)

问题

我在逐步向列表中添加元素时遇到了困难。

这是一个最小化工作示例(MWE):

# 给定一个嵌套的值列表或集合
sets = [[1, 2, 3], [1, 2, 4], [1, 2, 5]]
        
# 将一个值添加到每个子列表,表示该集合在列表中的编号。
n_sets = len(sets)
for s in range(n_sets):
    (sets
展开收缩
).insert(0, s)
# 现在重复这些集合reps次 reps = 4 expanded_sets = [item for item in sets for i in range(reps)] # 然后为每个集合的每次出现分配一个重复编号。 rep_list = list(range(reps)) * n_sets for i in range(n_sets * reps): (expanded_sets[i]).insert(0, rep_list[i]) expanded_sets

这段代码返回的结果是:

[[3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 2, 1, 2, 5],
 [3, 2, 1, 0, 2, 1, 2, 5],
 [3, 2, 1, 0, 2, 1, 2, 5],
 [3, 2, 1, 0, 2, 1, 2, 5]]

而不是期望的结果:

[[0, 0, 1, 2, 3],
 [1, 0, 1, 2, 3],
 [2, 0, 1, 2, 3],
 [3, 0, 1, 2, 3],
 [0, 1, 1, 2, 4],
 [1, 1, 1, 2, 4],
 [2, 1, 1, 2, 4],
 [3, 1, 1, 2, 4],
 [0, 2, 1, 2, 5],
 [1, 2, 1, 2, 5],
 [2, 2, 1, 2, 5],
 [3, 2, 1, 2, 5]]

希望这次的回答对你有所帮助。

英文:

I'm having difficulty adding to a list iteratively.

Here's a MWE:

# Given a nested list of values, or sets
sets = [[1, 2, 3], [1, 2, 4], [1, 2, 5]]
    
# add a value to each sublist giving the number of that set in the list.
n_sets = len(sets)
for s in range(n_sets):
    (sets
展开收缩
).insert(0, s) # Now repeat those sets reps times reps = 4 expanded_sets = [item for item in sets for i in range(reps)] # then assign a repetition number to each occurance of a set. rep_list = list(range(reps)) * n_sets for i in range(n_sets * reps): (expanded_sets[i]).insert(0, rep_list[i]) expanded_sets

which returns

[[3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 0, 1, 2, 3],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 1, 1, 2, 4],
 [3, 2, 1, 0, 2, 1, 2, 5],
 [3, 2, 1, 0, 2, 1, 2, 5],
 [3, 2, 1, 0, 2, 1, 2, 5],
 [3, 2, 1, 0, 2, 1, 2, 5]]

instead of the desired

[[0, 0, 1, 2, 3],
 [1, 0, 1, 2, 3],
 [2, 0, 1, 2, 3],
 [3, 0, 1, 2, 3],
 [0, 1, 1, 2, 4],
 [1, 1, 1, 2, 4],
 [2, 1, 1, 2, 4],
 [3, 1, 1, 2, 4],
 [0, 2, 1, 2, 5],
 [1, 2, 1, 2, 5],
 [2, 2, 1, 2, 5],
 [3, 2, 1, 2, 5]]

Just for fun, the first loop returns an expected value of sets

[[0, 1, 2, 3], [1, 1, 2, 4], [2, 1, 2, 5]]

but after the second loop sets changed to

[[3, 2, 1, 0, 0, 1, 2, 3], [3, 2, 1, 0, 1, 1, 2, 4], [3, 2, 1, 0, 2, 1, 2, 5]]

I suspect the issue has something to do with copies and references. I've tried adding .copy() and slices in various places, but with the indexed sublists I haven't come across a combo that works. I'm running Python 3.10.6.

Thanks for looking!

Per suggested solution, [list(range(reps)) for _ in range(n_sets)] doesn't correctly replace the list(range(reps)) * n_sets, since it gives [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] instead of
the desired [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]. Do I need to flatten, or is there a syntax with the _ notation that gives me a single list?

Further update . . .
replacing

rep_list = list(range(reps)) * n_sets

with

rep_list_nest = [list(range(reps)) for _ in range(n_sets)]
rep_list = [i for sublist in rep_list_nest for i in sublist]

gives the same undesired result for expanded_sets.

答案1

得分: 1

问题出在这里:

expanded_sets = [item 
                 for item in sets
                 for i in range(reps)]

现在,这个列表中连续包含了sets中的每个元素四次,然后是下一个元素重复四次,以此类推。

创建item的副本可以修复这个问题:

expanded_sets = [item.copy() 
                 for item in sets
                 for i in range(reps)]

在线尝试

如果你想要一个更符合Python风格的方法,那就认识到结果是两个范围的乘积,并且是你原始的sets列表中所有元素的连接:

from itertools import product

sets = [[1, 2, 3], [1, 2, 4], [1, 2, 5]]

expanded_sets = [[inner_counter, outer_counter] + sets_elem
                  for sets_elem, outer_counter, inner_counter in product(sets, range(len(sets)), range(4))]

在线尝试

英文:

The problem is here:

expanded_sets = [item 
                 for item in sets
                 for i in range(reps)]

This list now contains the same element of sets four times in a row, followed by the next element repeated four times, and so on.

Creating copies of item fixes the issue:

expanded_sets = [item.copy() 
                 for item in sets
                 for i in range(reps)]

Try it online

If you want a more pythonic approach, then recognize that the result is a product of two ranges, and your original sets all concatenated together:

from itertools import product

sets = [[1, 2, 3], [1, 2, 4], [1, 2, 5]]

expanded_sets = [[inner_counter, outer_counter] + sets_elem
                  for sets_elem, outer_counter, inner_counter in product(sets, range(len(sets)), range(4))]

Try it online

huangapple
  • 本文由 发表于 2023年2月8日 12:28:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381387.html
匿名

发表评论

匿名网友

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

确定