python列表中的冒号和逗号是什么? [英] What does a colon and comma stand in a python list?

查看:1074
本文介绍了python列表中的冒号和逗号是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python脚本list[:, 1]中遇到了这个问题,我试图弄清楚逗号的作用.

I met this in a python script list[:, 1] and I am trying to figure out the role of the comma.

推荐答案

一般来说:

foo[somestuff]

调用__getitem____setitem__. (还有__getslice____setslice__,但现在已弃用,因此,我们不再赘述).现在,如果somestuff中包含逗号,则python会将tuple传递给基础函数:

calls either __getitem__, or __setitem__. (there's also __getslice__ and __setslice__, but those are now deprecated, so let's not talk about that). Now, if somestuff has a comma in it, python will pass a tuple to the underlying function:

foo[1,2]  # passes a tuple

如果存在:,则python将传递一个切片:

If there is a :, python will pass a slice:

foo[:]  # passes `slice(None, None, None)`
foo[1:2]  # passes `slice(1, 2, None)`
foo[1:2:3]  # passes `slice(1, 2, 3)
foo[1::3]  # passes `slice(1, None, 3)

希望您能理解.现在,如果有逗号和冒号,python将传递一个包含切片的元组.在您的示例中:

Hopefully you get the idea. Now if there is a comma and a colon, python will pass a tuple which contains a slice. in your example:

foo[:, 1]  # passes the tuple `(slice(None, None, None), 1)`

对象(foo)对输入所做的操作完全取决于对象.

What the object (foo) does with the input is entirely up to the object.

这篇关于python列表中的冒号和逗号是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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