Python 练习实例33
题目:按逗号分隔列表。
程序分析:无。
实例(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
题目:按逗号分隔列表。
程序分析:无。
以上实例输出结果为:
1,2,3,4,5
匿名
129***[email protected]
参考方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- L = [1, 2, 3, 4, 5] L = repr(L)[1:-1] print L
匿名
129***[email protected]
Webben
wei***[email protected]
参考:
# -*- 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]
Webben
wei***[email protected]
参考:
#!/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]
滑稽树上滑稽果
374***[email protected]
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]
爱吃零食的瘦子
239***[email protected]
Python3 测试:
numbers=list(range(1,9)) for i in numbers: if(i==numbers[-1]): print(i) else: print(i,end=',')
爱吃零食的瘦子
239***[email protected]
无畏布衣
ren***[email protected]
参考:
import re l=str(range(5)); m=re.search(r'\[(.*?)\]',l) print( m.groups()[0] )
无畏布衣
ren***[email protected]