如何访问列表元素

我有一个列表

list = [['vegas','London'],['US','UK']] 

如何访问此列表的每个元素?

因为这是Python的内置listtypes的构造函数的名称,所以我不开始调用它。

但是一旦你把它重新命名为cities或者其他东西,你可以这样做:

 print(cities[0][0], cities[1][0]) print(cities[0][1], cities[1][1]) 

学习Python 34的困难之路

尝试这个

 animals = ['bear' , 'python' , 'peacock', 'kangaroo' , 'whale' , 'platypus'] # print "The first (1st) animal is at 0 and is a bear." for i in range(len(animals)): print "The %d animal is at %d and is a %s" % (i+1 ,i, animals[i]) # "The animal at 0 is the 1st animal and is a bear." for i in range(len(animals)): print "The animal at %d is the %d and is a %s " % (i, i+1, animals[i]) 

recursion解决scheme打印列表中的所有项目:

 def printItems(l): for i in l: if isinstance(i,list): printItems(i) else: print i l = [['vegas','London'],['US','UK']] printItems(l) 

这很简单

 y = [['vegas','London'],['US','UK']] for x in y: for a in x: print(a)