我有一个清单:
list = [['vegas','London'], ['US','UK']]
如何访问该列表的每个元素?
my_list = [['vegas', 'London'], ['US', 'UK']] for sublist in my_list: for item in sublist: print(item)
输出:
vegas London US UK
my_list = [['vegas', 'London'], ['US', 'UK']] flattened_list = [item for sublist in my_list for item in sublist] print(flattened_list)
['vegas', 'London', 'US', 'UK']
my_list = [['vegas', 'London'], ['US', 'UK']] print(my_list[0][0]) # Access 'vegas' print(my_list[0][1]) # Access 'London' print(my_list[1][0]) # Access 'US' print(my_list[1][1]) # Access 'UK'