Getting the first x item from a list
Possible Duplicate:
Good Primer for Python Slice Notation
I have a string and I'm splitting in a ;
character, I would like to associate this string with variables, but for me just the first x strings is useful, the other is redundant;
I wanted to use this code below, but if there is more than 4 coma than this raise an exception. Is there any simply way?
az1, el1, az2, el2, rfsspe = data_point.split(";")
Yes! Use slicing:
az1, el1, az2, el2, rfsspe = data_point.split(";")[:5]
That "slices" the list to get the first 5 elements only.
方式,我这样做通常是将所有变量添加到列表(var_list),然后当我处理列表时,我做了类似
for x in var_list[:5]:
print x #or do something
链接地址: http://www.djcxy.com/p/26738.html
上一篇: 扩展切片语法实际上对负向步骤做什么?
下一篇: 从列表中获取第一个x项目