子列表中的切片元素-Python [英] Slicing element from sublist - Python

查看:149
本文介绍了子列表中的切片元素-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从中返回数字5:

list_1 = [[1, 2, 3], [4, 5, 6]]

我以为这可以用,但是不行.

I thought this would work but it is not:

print(list_1[1:1])

它返回一个空列表.它是索引1(第二个列表)和位置1(第二个列表).

It returns an empty list. It is Index 1 (second list) and position 1 (second number in the list).

那行不通吗?

推荐答案

您需要两个单独的操作:

You need two separate operations:

sub_list = list_1[1]
item = sub_list[1]
# or shortly list_1[1][1]

您所做的是调用切片接口,该接口的接口为[from:to:step].因此,它的意思是:给我所有从 索引1到 index 1的项目"(有关切片的更多信息,请阅读.)

What you did was calling the slice interface which has an interface of [from:to:step]. So it meant: "give me all the items from index 1 to index 1" (read about slicing for more information).

这篇关于子列表中的切片元素-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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