简体   繁体   English

汇总列表中每隔四个元素

[英]Summing up every alternate 4 elements in list

I am new to python, I have very long list (around 100 elements in list). 我是python的新手,列表很长(列表中约有100个元素)。 I would like to add every 4th element of this list and store result as sum1 , sum2 , sum3 , or sum4 . 我想添加此列表的第4个元素,并将结果存储为sum1sum2sum3sum4 I used the following code : 我使用以下代码:

while y in range(0,104):
    if y%4==0:
    sum=last_indexes[y]+sum
    y=y+4

Questions: 问题:

  1. If the lists increases the compilation time increases. 如果列表增加,则编译时间会增加。 Is there any function in python by which I can fasten this process? python中是否有任何我可以固定此过程的功能?
  2. Also if the list is not a multiple of 4 and still I want to store it in sum1,sum2,sum3,sum4 then what modification should be done. 另外,如果列表不是4的倍数,但我仍然想将其存储在sum1,sum2,sum3,sum4中,则应进行哪些修改。

Thanks for your time and consideration 感谢您的时间和考虑

No, you cannot make code run (not compile) at the same speed if the input is larger. 不可以,如果输入较大,则无法使代码以相同的速度运行(不编译)。

The more Pythonic way to handle this problem, which should work even if the length of the list is not a multiple of 4 is to use list slicing: 处理列表问题的更Python化方法,即使列表的长度不是4的倍数,该方法也应起作用,这是使用列表切片:

sum1 = sum(last_indices[::4])
sum2 = sum(last_indices[1::4])
sum3 = sum(last_indices[2::4])
sum4 = sum(last_indices[3::4])

The slice notation of a:b:c says "Start at index a , go to index b , in increments of c ". a:b:c的切片符号表示“从索引a开始,以c递增到索引b ”。 When a is omitted, it is assumed to be 0 . a被省略,它被假定为0 when b is omitted, it is assumed to be the end of the list. 当省略b时,假定为列表的末尾。

For a fast and clean solution, use itertools.islice() to extract the data elements at fixed intervals: 为了获得快速,干净的解决方案,请使用itertools.islice()以固定的时间间隔提取数据元素:

from itertools import islice

multiple = 4
for start in range(multiple):
    print sum(islice(data_array, start, None, multiple))

For large lists, this is fast because: 对于大型列表,这是快速的,因为:

  • it doesn't do expensive modulo computations (islice has an internal counter) 它不执行昂贵的模运算(islice有一个内部计数器)
  • it doesn't build temporary lists like regular slices do (islice uses iterators) 它不像常规切片那样构建临时列表(islice使用迭代器)
  • both sum() and islice() run at C speed (that is the whole point of the itertools module) sum()islice()均以 C速度运行(这是itertools模块的重点)

Any time you have a series of variables named like varX where X is a number, you should probably use a list, rather than multiple variables (exceptions may exist for very small numbers of variables, like two or maybe three). 每当您有一系列名为varX的变量(其中X是一个数字)时,您可能应该使用列表,而不是多个变量(极少数变量(例如两个或三个)可能会存在例外)。 In your situation, you should probably use a list with four integers, rather than four separate sumX variables. 在您的情况下,您可能应该使用包含四个整数的列表,而不是四个单独的sumX变量。 As a side benefit, this will allow your code to be very simple! 作为附带的好处,这将使您的代码非常简单!

sums = [0]*4                  # initialize list of sums
for i, v in enumerate(lst):   # iterate over indexes and values from the source list
    sums[i%4] += v            # add each value to the appropriate sum

You could you a "for loop" for that. 您可以为此设置一个“ for循环”。 For loops are very commonly used for list management. For循环非常常用于列表管理。 One that would solve your problem could be like this: 一个可以解决您的问题的方法可能是这样的:

for i in range(len(your_list)-1):
    if i % 4 == 0:
        sum = your_list[i] + sum

Use the following code: 使用以下代码:

>>> lst = [5, 7, 3, 5, 4, 2, 7, 9, 5, 6, 1, 3, 2, 7, 5, 7, 5, 2, 9, 8, 9, 5, 3, 6, 2]
>>> len(lst)
25
>>> sum1 = sum2 = sum3 = sum4 = 0
>>> for k in range(len(lst)):
...     i = k % 4
...     j = lst[k]    
...     if i == 1:
...             sum1+=j
...     elif i == 2:
...             sum2+=j
...     elif i == 3:
...             sum3+=j
...     elif i == 0:
...             sum4+=j
... 
>>> sum1
29
>>> sum2
28
>>> sum3
38
>>> sum4
32
>>> 

Note: I have truncated lst to a length of 25 for demonstration purposes 注意:出于演示目的,我已将lst截短为25

We first assign lst to be our desired list. 我们首先将lst分配为我们想要的列表。 We then make sum1 , sum2 , sum3 , and sum4 integers with a value of 0. We loop over the range of the list, and using modulo, assign i to be the remainder of k/4 . 然后,使sum1sum2sum3sum4整数值为0。我们遍历列表的范围,并使用模,将i分配为k/4的余数。

>>> 5 % 4
1
>>> 10 % 4
2
>>> 23 % 4
3
>>> 16 % 4
0

According to the value of i , we add i to a specific integer. 根据i的值,我们将i加到一个特定的整数。 We then print it out. 然后我们将其打印出来。

If you want really fast one. 如果您真的想要快速。 This is the fastest way I think. 这是我认为最快的方法。 This will raise Exception ,but there is no problem in the result. 这将引发Exception,但结果没有问题。 If you don't want to finish with Exception, you can use try...except block like myfunc in measurement code below. 如果您不想以Exception结尾,可以在下面的测量代码中使用try...except块,例如myfunc

it = iter(data_array)
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
while(1):
    sum1 += it.next()
    sum2 += it.next()
    sum3 += it.next()
    sum4 += it.next()

Measurement code: 测量代码:

#my answer
def myfunc(data_array):
    it = iter(data_array)
    sum1 = 0
    sum2 = 0
    sum3 = 0
    sum4 = 0
    while(1):
        try:
            sum1 += it.next()
            sum2 += it.next()
            sum3 += it.next()
            sum4 += it.next()
        except:
            break

#@Blckknght answer
def myfunc2(data_array):
    sums = [0]*4
    for i, v in enumerate(data_array):
        sums[i%4] += v

#@aj8uppal answer
def myfunc3(data_array):
    sum1 = 0
    sum2 = 0
    sum3 = 0
    sum4 = 0
    for k in range(len(data_array)):
        i = k % 4
        j = data_array[k]
        if i == 1:
                sum1+=j
        elif i == 2:
                sum2+=j
        elif i == 3:
                sum3+=j
        elif i == 0:
                sum4+=j

#@Raymond Hettinger answer
from itertools import islice
def myfunc4(data_array):
    sum1 = sum(islice(data_array, 0, None, 4))
    sum2 = sum(islice(data_array, 1, None, 4))
    sum3 = sum(islice(data_array, 2, None, 4))
    sum4 = sum(islice(data_array, 3, None, 4))

#@merlin2011 answer
def myfunc5(data_array):
    sum1 = sum(data_array[::4])
    sum2 = sum(data_array[1::4])
    sum3 = sum(data_array[2::4])
    sum4 = sum(data_array[3::4])

Measurement: 测量:

>>> data_array = range(100)

>>> %timeit myfunc(data_array)
100000 loops, best of 3: 12.5 us per loop

>>> %timeit myfunc2(data_array)
100000 loops, best of 3: 14.6 us per loop

>>> %timeit myfunc3(data_array)
100000 loops, best of 3: 16.7 us per loop

>>> %timeit myfunc4(data_array)
10000 loops, best of 3: 33.3 us per loop

>>> %timeit myfunc5(data_array)
10000 loops, best of 3: 56 us per loop

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

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