菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例33

Python 100例 Python 100例

题目:按逗号分隔列表。

程序分析:无。

实例(Python 2.0+)

#!/usr/bin/python# -*- coding: UTF-8 -*-L = [1,2,3,4,5]s1 = ','.join(str(n)forninL)print(s1)

以上实例输出结果为:

1,2,3,4,5

Python 100例 Python 100例

AI 思考中...

6 篇笔记 写笔记

  1. #0

    匿名

    129***[email protected]

    23

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    L = [1, 2, 3, 4, 5]
    L = repr(L)[1:-1]
    print L

    匿名

    129***[email protected]

    9年前 (2017年08月02日)
  2. #0

    Webben

    wei***[email protected]

    8

    参考:

    # -*- coding: UTF-8 -*-
    l = [1,2,3,4,5,6,7];
    k = 1;
    for i in l:
     print(i,end= ('' if (k == len(l)) else ','));
     k += 1;

    Webben

    wei***[email protected]

    9年前 (2017年08月03日)
  3. #0

    Webben

    wei***[email protected]

    4

    参考:

    #!/usr/bin/python3
    l = [1,2,3,4,5,6,7];
    o = '';
    for i in l:
     o += str(i)+',';
    print(o[0:-1]);

    Webben

    wei***[email protected]

    9年前 (2017年08月03日)
  4. #0

    滑稽树上滑稽果

    374***[email protected]

    3

    Python3 测试:

    a=[1,2,3,4]
    for i in range(0,len(a)):
     if i!=(len(a)-1):
     print(a[i],end=',')
     else:
     print(a[i])

    滑稽树上滑稽果

    374***[email protected]

    9年前 (2017年11月11日)
  5. #0

    爱吃零食的瘦子

    239***[email protected]

    2

    Python3 测试:

    numbers=list(range(1,9))
    for i in numbers:
     if(i==numbers[-1]):
     print(i)
     else:
     print(i,end=',')

    爱吃零食的瘦子

    239***[email protected]

    9年前 (2017年11月23日)
  6. #0

    无畏布衣

    ren***[email protected]

    1

    参考:

    import re
    l=str(range(5));
    m=re.search(r'\[(.*?)\]',l)
    print( m.groups()[0] )

    无畏布衣

    ren***[email protected]

    9年前 (2017年12月14日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /