Python小练习(持续更新....)

最近一直在学习python,这些小练习有些是书上的,有些是别人博客上的!

# 1.题目1
# 给一个字符串,统计其中的数字、字母和其他类型字符的个数;
# 比如输入“124mid-=”,输出:数字=3,字母=3,其他=2。

 1 #coding-utf8
 2 # 1.题目1
 3 # 给一个字符串,统计其中的数字、字母和其他类型字符的个数;
 4 # 比如输入“124mid-=”,输出:数字=3,字母=3,其他=2。
 5 
 6 #--------------------------------例子----------------------
 7 # s=input("请输入..")
 8 # print(s.isdigit())  # 用isdigit函数判断是否数字
 9 # print(s.isalpha())  # isalpha判断是否字母
10 # print(not (s.isalpha() or s.isdigit()) and s.isalnum())  # isalnum判断是否数字和字母的组合
11 
12 #----------------------------------------end----------------------------
13 
14 #定义初始化变量
15 strcount=0
16 intcount=0
17 othercount=0
18 s=input("请输入..")
19 for i in  s :
20     if i.isdigit() :
21         intcount+=1
22     elif i.isalpha() :
23         strcount+=1
24     else:
25         othercount+=1
26 print("字母=%d,数字=%d,其他的=%d"%(strcount,intcount,othercount))

题目2

1 # 题目2:删除列表中重复的元素
2 # 如果列表中有重复的元素,我们想要删除重复的
3 li = [1, 2, 3, 4, 5, 2, 1, 3, 4, 57, 8, 8, 9]
4 print(li)#打印下
5 for x in  li :
6      while li.count(x) > 1 :
7          li.remove(x)
8 
9 print(li)

 方法很多,我比较懒,选择了最简单的一种方式!

 

 

 



posted @ 2017-04-11 14:13  指针怒草内存栈  阅读(241)  评论(0编辑  收藏  举报