繁体   English   中英

如何迭代列表并加入 Python 中的特定元素?

[英]How to iterate a list and join specific elements in Python?

假设我们有一个列表l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...]我想对该列表中的元素进行分组,以便:

(1, 5, 9)
(2, 6, 10)
(3, 7, 11)
(4, 8, 12)

等等。 (考虑到列表的长度总是3的倍数)

尝试了以下方法,但当然这只是连续对元素进行分组:

>>>for x, y, z in itertools.zip_longest(*[iter(lista)] * 3):
    print(x,y,z)
>>>(1 2 3)

(4 5 6)

(7 8 9)

(10 11 12)

所需的 output: (1, 5, 9) (2, 6, 10) (3, 7, 11) (4, 8, 12)

更新的问题让我将问题“升级”到我的具体方法:所以,我有 900 个 numpy arrays 的列表,我想将这些 arrays 分组,因此将有一个包含 3.9CZ 的子列表的最终长度300个子列表,每个子列表包含3个arrays。 这些 arrays 应按上述方式分组。 如果我们有 900 个数字的列表,所需的 output 应该是: (1, 5, 9) (2, 6, 10) (3, 7, 11) (4, 8, 12) (13, 17, 21) (14, 18, 22) (15, 19, 23) (16, 20, 24) (25, 29, 33) (26, 30, 34) (27, 31, 35) (28, 32, 36)...

您可以使用列表推导,将列表大小除以步长的次数进行切片:

step = 4
[l[i::step] for i in range(len(l)//(step-1))]
# [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

原始答案

您可以使用list slicing和简单for循环在一个范围内实现相同的目的

# Input
set_of_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
# Capture every 4th element in a sequence starting from 0th index, hence set number_of_rows = 4
number_of_rows = 4
# Loop through the range(4) --> [0,1,2,3] and each time collect data by jumping 4 positions
res = [set_of_data[i::number_of_rows] for i in range(number_of_rows)]
# Printing result
print (res)
# Output
# [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

我希望这会有所帮助并且很重要!

根据要求更新了答案。

    # Input
set_of_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14,15,16,17,18, 19,20,21,22,23,24]
# Defining variables
len_of_data = len(set_of_data)
number_of_rows = 4
number_of_group = 12

# Main code
res = [ tuple(set_of_data[i:i+number_of_group][j::number_of_rows])  for  i in range(0,len_of_data, number_of_group) for j in range(number_of_rows) ]

# Output
print (res)
# [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12), (13, 17, 21), (14, 18, 22), (15, 19, 23), (16, 20, 24)]

如果您对任何答案感到满意,我要求您关闭答案。

最终答案:

如果您不想重复元素,请尝试以下操作:

import numpy as np
l = list(range(1, 301))
step = 4
length = 3
size = step * length
result = np.array(l).reshape(len(l) // size, length, step).transpose(0, 2, 1).reshape(len(l) // length, length)

结果:

array([[  1,   5,   9],
       [  2,   6,  10],
       [  3,   7,  11],
       [  4,   8,  12],
       [ 13,  17,  21],
       [ 14,  18,  22],
       [ 15,  19,  23],
       [ 16,  20,  24],
       [ 25,  29,  33],
       ...
       [277, 281, 285],
       [278, 282, 286],
       [279, 283, 287],
       [280, 284, 288],
       [289, 293, 297],
       [290, 294, 298],
       [291, 295, 299],
       [292, 296, 300]])

更新的原始答案:

根据您明确的要求,您可以这样做:

step = 4
result = [l[i:i+step*2+1:step] for i in range(len(l)-step*2)]

结果:

[[1, 5, 9], 
 [2, 6, 10], 
 [3, 7, 11], 
 ...
 [290, 294, 298],
 [291, 295, 299],
 [292, 296, 300]]

解释:

step = 4  # the spacing between each element in the     
                                            # ┌ loop from 0 to 2 steps before list ends (300 - 8 = 292)
result = [l[i:i+step*2+1:step] for i in range(len(l)-step*2)]
#         │ │ │          └ skip every step (4)
#         │ │ └ end after step occured twice (8); the +1 is needed because end index is not returned.
#         │ └ start from index i (0, 1, 2,... 291, 292)
#         └ slice list l into a triplet

原答案:

看到您已经在使用numpy来处理 900 个元素,您可以这样做:

np.array(l).reshape(3, len(l)//3).T

这给了你:

array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

如果您需要将其作为list返回,您可以执行.tolist()

np.array(l).reshape(3, len(l) //3).T.tolist()
# [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM