列表上有两个嵌套的for循环 [英] two nested for loops on a list

查看:75
本文介绍了列表上有两个嵌套的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在列表的元素上创建两个嵌套的for循环,以使第二个循环在第一个循环之后的元素上开始.例如,我知道如何在range()上进行操作:

how to create two nested for loops on elements of a list such that the second loop starts on elements after the first loop. For example I know how to to it on range():

    for i in range(0,3):
       for j in range(i+1,3):
           print([i,j])

如果我有一个列表A = [1,4,7,3]而不是range(0,3),我想做同样的事情怎么办?

what if I have a list A=[1,4,7,3] instead of range(0,3) and I want to do the same thing?

谢谢您的帮助.

推荐答案

您可以使用 len()方法.

for i in range(0, len(listA)):
   for j in range(i+1,len(listA)):
       print(listA[j])

这篇关于列表上有两个嵌套的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆