这个是我很久以前学习python时候自己给自己出的练习题,不是很难,但是却综合锻炼了列表,if语句循环及遍历,以及写文件等,算是python基础的一个总结,如果能顺利写出来,差不多能算是python入门了吧
![python入门测试 列表内重复数计数并输出](http://p2.ttnews.xyz/loading.gif)
python
list = ['0','1','2','2','3','3','3','4','4','4','4']
要求输出:
2 重复了 2 次
3 重复了 3 次
4 重复了 4 次
代码如下:
list = ['0','1','2','2','3','3','3','4','4','4','4']
a = []
for i in list:
if list.count(i) > 1:
a.append(i)
lista = set(a)
c = []
for x in lista:
j = list.count(x)
c.append('%s 重复了 %s 次' % (x, j) + '\n')
with open ('result.txt', 'w') as re:
re.writelines(c)
代码输出结果如下:
![python入门测试 列表内重复数计数并输出](http://p2.ttnews.xyz/loading.gif)
python 入门测试
各位想入门的觉得自己可以写出来吗?
python 入门
閱讀更多 aovs 的文章