English 中文(简体)
在Python的列表上进行总结 - 有没有更好的办法?
原标题:Summing over lists in python - is there a better way?

是否有更明智的方法来做到这一点? 我想通过对许多其他列表的索引进行总结来做一个新的列表。 我对编程相当新,这似乎是一个非常笨拙的方法!

list1 = [1,2,3,4,5]
list2 = [1,1,1,4,1]
list3 = [1,22,3,1,5]
list4 = [1,2,5,4,5]
...
list100 = [4,5,6,7,8]

i = 0
while i < len(list1):
    mynewlist[i] = list1[i]+list2[i]+list3[i]+list4[i]+...list100[i]
    i = i+1
最佳回答
问题回答

@Shawns 答对了, 我认为map 可能比列表理解更优雅:

>>> list1 = [1,2,3,4,5]
>>> list2 = [1,1,1,4,1]
>>> list3 = [1,22,3,1,5]
>>> list4 = [1,2,5,4,5]
>>> map(sum, zip(list1, list2, list3, list4))
[4, 27, 12, 13, 16]

首先的问题是: 您的代码中不应该有 100 个变量 。

list1 = [1,2,3,4,5]
list2 = [1,1,1,4,1]
list3 = [1,22,3,1,5]
list4 = [1,2,5,4,5]
...
list100 = [4,5,6,7,8]

相反,你应该有类似

list_of_lists = []
list_of_lists.append([1,2,3,4,5])
list_of_lists.append([1,1,1,4,1])
list_of_lists.append([1,22,3,1,5])
list_of_lists.append([1,2,5,4,5])
...

然后您计算出想要的结果 :

[sum(x) for x in zip(*list_of_lists)]

您真的需要阅读 Python 教程 。

  1. sum(list1) will give you the sum of that list.
  2. You need to learn about for loops
  3. Your lists should themselves be stored in a list
  4. 旋转列表集的方式是使用 zip :

    list1 = [1,2,3,4,5]
    list2 = [1,1,1,4,1]
    list3 = [1,22,3,1,5]
    zip(list1,list2,list3)
    # matches up element n of each list with element n of the other lists
    #=> [(1, 1, 1), (2, 1, 22), (3, 1, 3), (4, 4, 1), (5, 1, 5)]
    # now you want to add up each of those tuples:
    [sum(t) for t in zip(list1,list2,list3)]
    #=> [3, 25, 7, 9, 11]
    

要使用 zip 这样的列表列表工作,请查看 etertools 模块,或学习 args 语法。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签