Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取列表中的准确数字_Python_List_Operators - Fatal编程技术网

Python 获取列表中的准确数字

Python 获取列表中的准确数字,python,list,operators,Python,List,Operators,我在python中搜索一些东西,以从列表中每隔4个数字返回一个结果 这里有一个例子: list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] I would like only [1,5,9,13,17] So every 4 elements of list It returns me the result ! 所以我想我可能需要使用操作符%,但我不知道如何才能做到 谢谢 试试这个: my_list=[1,2,3,4,5,6,7,8,9,10,

我在python中搜索一些东西,以从列表中每隔4个数字返回一个结果

这里有一个例子:


list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]

I would like only [1,5,9,13,17]

So every 4 elements of list It returns me the result !
所以我想我可能需要使用操作符%,但我不知道如何才能做到

谢谢

试试这个:

my_list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
print(my_list[0::4])
而且
list
不是变量的好名字,因为它是python中的内置函数。

我会使用:

list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
list[::4]

这回答了你的问题吗<代码>列表[::4]-遍历列表中的每四个元素。另外,
list
是一个关键字,所以不要将其用作变量名