这里的非常基本的问题(我刚刚开始露天)。
我有一份清单反对。 它包含5个数字[3,6,2,3,1]
我想名单上第一、第三和第四位数字的总和。
The syntax?
这里的非常基本的问题(我刚刚开始露天)。
我有一份清单反对。 它包含5个数字[3,6,2,3,1]
我想名单上第一、第三和第四位数字的总和。
The syntax?
例如,你可以将第1、第3和第4项内容与灵活表述结合起来。
sum(my_list[i] for i in (0, 2, 3))
第一个要素的指数为0[而不是1],即:my_list[0]
是第一个要素(在原始问题中,价值3),等等。
清单中的项目编号如下:
a = [3, 6, 2, 3, 1]
^ ^ ^ ^ ^
index 0 1 2 3 4
用索引i
进入该项目,使用a[i]
。 从此,你就应当能够说明如何对所期望的项目进行总结。
仅用括号书写索引。 注:该指数以零开始:
lst[0] + lst[2] + lst[3]
在某些情况下,您可使用sum/code><
功能,并选定slice。 例如,为了获得第一、第三和第五部分的总和,使用:
sum(lst[::2])
您可以通过发送<代码>[清单_index]进入清单标的(replace list_index
,与你想要的索引一起查阅
my_list_object = [3,6,2,3,1]
my_sum = my_list_object[0]+my_list_object[2]+my_list_object[3]
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ]="...