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

有个字符串 有很多个逗号隔开的,但是我想去除最后一个逗号,如何解决

  •  
  •   bestehen · 2018-07-30 15:51:24 +08:00 · 9101 次点击
    这是一个创建于 2068 天前的主题,其中的信息可能已经有所发展或是发生改变。
    s=‘ a,b,c,d,’变成‘ a,b,c,d ’
    26 条回复    2018-08-03 14:25:23 +08:00
    xubeiyan
        1
    xubeiyan  
       2018-07-30 16:18:40 +08:00   ❤️ 1
    s=s[:-1]
    用 rstrip(',')也行
    bestehen
        2
    bestehen  
    OP
       2018-07-30 16:26:12 +08:00
    @xubeiyan 是字符串 ,你的第一个是列表吧,我说的是字符串
    ballshapesdsd
        3
    ballshapesdsd  
       2018-07-30 16:32:25 +08:00
    @bestehen #2 字符串也可以这样用啊。。你这也太萌新了吧
    xubeiyan
        4
    xubeiyan  
       2018-07-30 19:31:55 +08:00   ❤️ 1
    >>> s = 'a,b,c,d,'
    >>> s[:-1]
    'a,b,c,d'
    >>> s.rstrip(',')
    'a,b,c,d'
    >>>
    samv2
        5
    samv2  
       2018-07-30 20:29:33 +08:00 via Android
    正则替换 (不会 python )

    ,([^,]*) $ ----> $1

    上面是 java 语法,$1 表示引用第一对括号里的值
    samv2
        6
    samv2  
       2018-07-30 20:30:14 +08:00 via Android
    正则替换 (不会 python )

    ,([^,]*) $ ----> $1

    上面是 java 正则语法,$1 表示引用第一对括号里的值
    infun
        7
    infun  
       2018-07-30 20:39:44 +08:00
    @bestehen 一楼正解
    tan90
        8
    tan90  
       2018-07-30 20:41:46 +08:00
    $a="12313,1,123,1,";
    $a_nu = strlen($a);
    if (substr($a,$a_nu-1,$a_nu)==","){
    $a=substr($a,0,$a_nu-1);
    }
    php,临时想的,可以过审吗?
    VDimos
        9
    VDimos  
       2018-07-30 20:44:12 +08:00 via Android
    split 后更改列表再 join 呗
    ry_wang
        10
    ry_wang  
       2018-07-30 20:49:31 +08:00
    awk -F',' '{ print $NF }'
    ry_wang
        11
    ry_wang  
       2018-07-30 20:50:34 +08:00
    - -! 理解错楼主的需求了。。。忽略楼上
    tan90
        12
    tan90  
       2018-07-30 20:54:12 +08:00
    s='a,b,c,d,'
    s_nu=len(s)
    if s[s_nu-1:s_nu]==',':
    s=s[0:s_nu-1]
    长度,判断,重新赋值
    我测试过,可以跑的通
    input2output
        13
    input2output  
       2018-07-30 20:57:56 +08:00
    str := "a,b,c,d,"
    str = str[:len(s)-1]
    frmongo
        14
    frmongo  
       2018-07-30 20:59:00 +08:00
    如下

    a="this,is,that,is,what"
    b=a.split(',')
    b.pop()
    c=','.join(b)
    liaohongxing
        15
    liaohongxing  
       2018-07-30 21:09:52 +08:00
    $newString = rtrim(‘ a,b,c,d,’, ‘,’);
    liaohongxing
        16
    liaohongxing  
       2018-07-30 21:10:24 +08:00
    php
    lhx2008
        17
    lhx2008  
       2018-07-30 21:16:18 +08:00
    @frmongo 为啥要 pop 一个
    jiqing
        18
    jiqing  
       2018-07-30 21:58:04 +08:00
    java 我还想了半天怎么实现,直接有个方法
    public static void main(String[] args) {

    String s="a,b,c,d,";
    System.out.println(s.substring(0, s.length()-1));

    }
    mec
        19
    mec  
       2018-07-30 22:58:20 +08:00
    [:-1]
    princelai
        20
    princelai  
       2018-07-31 00:44:23 +08:00 via Android
    一楼就是正解啊,py 里可迭代对象都能切片操作
    yxcoder
        21
    yxcoder  
       2018-07-31 11:06:25 +08:00
    字符串去掉最后一个字符吧
    TonyLiu2ca
        22
    TonyLiu2ca  
       2018-07-31 11:25:40 +08:00
    题主题目所述与题内所给示例有出入,造成歧义

    按照题目的意思是:去除最后一个逗号。那么字符串可能是:a,b,c,d, 也可能是 a,b,c,d,ef
    也就是说最后一个逗号后面可能有也可能没有其它非逗号字符(#14 楼考虑了这种情况)

    题主没有给出如果有其它字符,去除最后一个逗号后,其它字符如何处理。有可能是全部删除,也有可能是只删除逗号字符本身。

    所以此题,暂时无解
    lixinyu1024
        23
    lixinyu1024  
       2018-07-31 13:19:42 +08:00
    python 的话
    str.strip(',')
    frmongo
        24
    frmongo  
       2018-07-31 13:56:38 +08:00
    @lhx2008 最后一个逗号后的丢弃
    daemonghost
        25
    daemonghost  
       2018-07-31 18:15:29 +08:00
    bash
    s=${s::-1}
    houzhiqiang
        26
    houzhiqiang  
       2018-08-03 14:25:23 +08:00
    a = "13asda,asda,"
    if a.endswith(","):
    a = a[:-1]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5425 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:09 · PVG 16:09 · LAX 01:09 · JFK 04:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.