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/5/spring-mvc/2.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
使用for循环打印特定于python的列表索引_Python_List_Loops - Fatal编程技术网

使用for循环打印特定于python的列表索引

使用for循环打印特定于python的列表索引,python,list,loops,Python,List,Loops,我有一个列表l1=[1,56,67,79,90,47,08,56,79,84,76,79,68] 现在,我想使用循环单独打印索引4,6,9,10 我试过: for i in l1: Print(i[4]..) 但是它说:int是不可下标的我假设这是一个家庭作业,所以你想使用循环来完成这个。 当你在l1中说i时,每个元素i都是一个int,因此索引它不起作用 如果您试图具体地打印索引4、6、9、10中的元素,那么您需要将这些元素放入一个列表中,并对它们进行迭代。例如: l1 =[1,56,

我有一个列表
l1=[1,56,67,79,90,47,08,56,79,84,76,79,68]

现在,我想使用循环单独打印索引4,6,9,10

我试过:

for i in l1:
    Print(i[4]..)

但是它说:
int是不可下标的

我假设这是一个家庭作业,所以你想使用循环来完成这个。 当你在l1中说
i时,每个元素
i
都是一个
int
,因此索引它不起作用

如果您试图具体地打印索引4、6、9、10中的元素,那么您需要将这些元素放入一个列表中,并对它们进行迭代。例如:

l1 =[1,56,67,79,90,47,08,56,79,84,76,79,68,]
to_print = [4, 6, 9, 10] # So if you want to print other/more index positions then modify this. Note that you may want to do a length check too before using these indexes as is.
for i in to_print:
    print(l1[i])

我假设这是一个家庭作业,所以你想使用循环。 当你在l1
中说
i时,每个元素
i
都是一个
int
,因此索引它不起作用

如果您试图具体地打印索引4、6、9、10中的元素,那么您需要将这些元素放入一个列表中,并对它们进行迭代。例如:

l1 =[1,56,67,79,90,47,08,56,79,84,76,79,68,]
to_print = [4, 6, 9, 10] # So if you want to print other/more index positions then modify this. Note that you may want to do a length check too before using these indexes as is.
for i in to_print:
    print(l1[i])

如果只想通过索引循环列表中的所有项目,可以执行以下操作:

l1 =[1,56,67,79,90,47,8,56,79,84,76,79,68]
for i in range(len(l1)):
   if i in (4, 6, 9, 10):
      print(l1[i])

也就是说,这不是最有效的方法

如果您只想通过索引循环列表中的所有项目,您可以执行以下操作:

l1 =[1,56,67,79,90,47,8,56,79,84,76,79,68]
for i in range(len(l1)):
   if i in (4, 6, 9, 10):
      print(l1[i])

也就是说,这不是最有效的事情

为什么你需要for循环?可能重复的是给定的任务broWhy你需要for循环吗?可能重复的是给定的任务broTkz有很多新的方法。哦,在答案的左侧,分数下面你会看到一个复选标记。点击它将其标记为已接受。Tkz有很多我是新来的方法哦,在答案的左侧分数下方,你会看到一个复选标记。点击它将其标记为已接受。是的,这也行。虽然我对这种方法的唯一评论是,使用命名列表比使用元组更好,以便于维护,是的,这也行。虽然我对这种方法的唯一评论是,使用命名列表比使用元组更好,以便于维护,