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
jtsr
V2EX  ›  Python

大家平时都怎么命名 split 后的变量

  •  
  •   jtsr · 2022-09-15 10:45:03 +08:00 · 3734 次点击
    这是一个创建于 560 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如 for line in f.readlines(): 第一种 line = line.split(' ') 第二种 line_lst = line.split(' ') 第三种 res = line.split(' ') or other

    35 条回复    2022-09-19 14:16:21 +08:00
    abc612008
        1
    abc612008  
       2022-09-15 10:46:18 +08:00   ❤️ 6
    parts
    wudicgi
        2
    wudicgi  
       2022-09-15 10:50:21 +08:00   ❤️ 4
    parts 就不错

    另外不建议用 lst, res 这种缩写

    比如 res 可以是
    result
    resource
    resolution
    response
    ……
    Leviathann
        3
    Leviathann  
       2022-09-15 10:51:26 +08:00   ❤️ 2
    words
    Akitora
        4
    Akitora  
       2022-09-15 10:51:55 +08:00
    idea 叫它 split ,那就 split 吧
    gydi
        5
    gydi  
       2022-09-15 10:52:35 +08:00
    line_iter
    cmdOptionKana
        6
    cmdOptionKana  
       2022-09-15 10:52:51 +08:00   ❤️ 1
    按照英语来说,line = lines.split('\n') 或 words = line.split(' ')
    Zenyet
        7
    Zenyet  
       2022-09-15 10:52:56 +08:00
    splitteds ? 。。。😂
    aaniao002
        8
    aaniao002  
       2022-09-15 11:15:20 +08:00 via Android
    无脑加 s
    optional
        9
    optional  
       2022-09-15 11:21:53 +08:00 via iPhone
    看你业务含义啊,
    Jirajine
        10
    Jirajine  
       2022-09-15 11:26:40 +08:00
    这种临时变量命名宜短不宜长
    jorneyr
        11
    jorneyr  
       2022-09-15 11:42:31 +08:00
    split 得到的数组业务用途是啥,最好名字和业务术语相关。
    wentx
        12
    wentx  
       2022-09-15 11:43:59 +08:00
    一样一样,splits
    bianjp
        13
    bianjp  
       2022-09-15 11:51:38 +08:00
    pieces
    likunyan
        14
    likunyan  
       2022-09-15 11:55:37 +08:00
    what is lst
    Runtime3
        15
    Runtime3  
       2022-09-15 11:57:27 +08:00 via Android
    line.split(' ').var ,自动生成,差球不多就行
    oneisall8955
        16
    oneisall8955  
       2022-09-15 11:59:32 +08:00
    xxxSplit
    MoYi123
        17
    MoYi123  
       2022-09-15 12:05:04 +08:00
    line.split(' ') 当然是 words
    calmzhu
        18
    calmzhu  
       2022-09-15 12:20:42 +08:00
    中间临时变量全部...
    _
    __
    ___
    licoycn
        19
    licoycn  
       2022-09-15 12:58:07 +08:00   ❤️ 2
    xxxList
    xxxArr
    xxxs
    xxxTemp
    CrazyRundong
        20
    CrazyRundong  
       2022-09-15 13:29:38 +08:00   ❤️ 1
    我的习惯是按照被拆分后元素自身的含义命名:
    rule_type, rule_arg, destination = "DOMAIN-NAME,google.com,Outside".split(",")

    如果没有特别指涉的字符串就叫 tokens:
    tokens = "foo,bar,baz".split(",")
    for token in tokens:
    ...
    chengyiqun
        21
    chengyiqun  
       2022-09-15 13:51:04 +08:00
    xxxSp
    v2exblog
        22
    v2exblog  
       2022-09-15 13:59:39 +08:00
    items ?
    otakustay
        23
    otakustay  
       2022-09-15 14:00:26 +08:00   ❤️ 1
    parts 、items 、segments 、fragments
    kongkongyzt
        24
    kongkongyzt  
       2022-09-15 15:59:56 +08:00
    chunks
    plams
        25
    plams  
       2022-09-15 17:21:58 +08:00
    line_split = line.split('\t')
    wanguorui123
        26
    wanguorui123  
       2022-09-15 17:23:48 +08:00
    var aArray=a.split('')
    keepeye
        27
    keepeye  
       2022-09-15 17:25:11 +08:00
    我一般用 xxxSegments
    tomwei7
        28
    tomwei7  
       2022-09-15 17:29:34 +08:00
    ss, vv, {xx}s
    abc0123xyz
        29
    abc0123xyz  
       2022-09-15 18:05:39 +08:00
    a1,a2,a3...an
    mritd
        30
    mritd  
       2022-09-15 18:08:45 +08:00
    ss = s.split(",")
    efaun
        31
    efaun  
       2022-09-16 04:42:58 +08:00
    使用 ide 提取变量功能的自动命名
    git00ll
        32
    git00ll  
       2022-09-16 10:56:27 +08:00
    a
    hhhhhh123
        33
    hhhhhh123  
       2022-09-16 15:42:45 +08:00
    个人习惯命名: 列表:xx_lst, 字典: xx_dit. 集合: xx_set. 所以 split 后是列表 , 故 xx_lst
    rev1si0n
        34
    rev1si0n  
       2022-09-16 17:25:19 +08:00
    如果是已知数量的 split ,看情况使用 a, b, c = xx.split(),如果每个字段有含义的话,会用含义来命名。未知数量,就像上面别人那样,看含义比如 words = xx.split()
    craiiz
        35
    craiiz  
       2022-09-19 14:16:21 +08:00
    非程序。看情况。像你这种,如果是预料到以后会再看, 我都是用 words 、words_list ;否则就是 ss, aa, bbbb 之类的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1167 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:29 · PVG 02:29 · LAX 11:29 · JFK 14:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.