V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lylehust
V2EX  ›  Python

请问关于 Python 中 string, list 的索引数字问题,和 range 函数

  •  1
     
  •   lylehust · 2018-11-21 12:14:27 +08:00 · 1878 次点击
    这是一个创建于 1976 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚开始学 python,以前用过 C 和 Matlab。学到 string 和 list 的索引数字时比较困惑。

    python 里是以元素的左边缘作为索引数字的位置,而 C 和 Matlab 直接以元素的位置作为索引数字。如下:

    x = ' P Y T H O N'
    Python:0 1 2 3 4 5 6
    C: 0 1 2 3 4 5

    这样,python 里 x[0:3]是‘ PYT',而 C 里 x[0:3]是'PYTH'。

    我比较疑惑 PYTHON 为什么这样定义,有什么好处或优势?因为我自己觉得 C 的定义更直观一些。

    另外,还有 range 函数,好像定义原理和索引数字一样。

    range(0, 4 )是 0,1,2,3,不包括 4,是指只能有 4 个数字?

    range(0, 10, 2)则是 0, 2, 4, 6, 8, 也不包括 10。

    同样的问题,为什么第二个参数不是实际边界,这样做的好处是什么?

    敬请大家帮我解惑,谢谢!
    9 条回复    2018-11-22 08:36:24 +08:00
    hsfzxjy
        1
    hsfzxjy  
       2018-11-21 12:20:18 +08:00 via Android   ❤️ 1
    如果你想从某个位置切开一个字符串,就可以不用考虑加减一的问题

    ```python
    s = 'abcdef'
    p = s.index('c')
    left, right = s[:p], s[p:]
    ```
    GeruzoniAnsasu
        2
    GeruzoniAnsasu  
       2018-11-21 12:22:20 +08:00
    C 没有 slice,没有[0:3]这种写法,每一个下标都对应那唯一一个位置

    slice 是左开右闭区间,
    (好处是循环迭代都是先验边界再进循环体,右闭在自增时能保证被迭代变量最终不会越界,顺带一提从 0 开始的索引也是为了自减时类似效果,顺带一提这点在初学编程时应该能感觉到)

    matlab 的索引规则跟大多数编程语言不同


    还有啥问题
    GeruzoniAnsasu
        3
    GeruzoniAnsasu  
       2018-11-21 12:23:21 +08:00
    说反了 左闭右开
    Eleflea
        4
    Eleflea  
       2018-11-21 12:30:53 +08:00   ❤️ 1
    python 里的 slice 和 range 都是包括左闭右开。
    一是这样做有:`s = s[:i] + s[i:]`
    二是 slice 的长度可以通过左右索引之差简单的算出来。
    lniwn
        5
    lniwn  
       2018-11-21 13:02:18 +08:00   ❤️ 3
    《流畅的 Python 》一书第 2.4.1 节对这个问题有解释,大致说了如下几个方面:





    上面提到的延伸阅读<http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html>
    ClutchBear
        6
    ClutchBear  
       2018-11-21 13:10:24 +08:00
    ![]( )
    我觉得试一下跟容易理解
    no1xsyzy
        7
    no1xsyzy  
       2018-11-21 14:28:53 +08:00
    One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:

    +---+---+---+---+---+---+
    | P | y | t | h | o | n |
    +---+---+---+---+---+---+
    0 1 2 3 4 5 6
    -6 -5 -4 -3 -2 -1

    <https://docs.python.org/3/tutorial/introduction.html#strings>
    ddzzhen
        8
    ddzzhen  
       2018-11-21 15:58:19 +08:00 via Android
    题主想的挺多也挺好,我是强记的,左闭右开
    Outliver0
        9
    Outliver0  
       2018-11-22 08:36:24 +08:00
    你会发现 random.randint 就包含声明数字了,嘿嘿
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2886 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:45 · PVG 21:45 · LAX 06:45 · JFK 09:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.