复习:
1.删除列表中重复的元素:l = [1,2,3,1,2,3]
法一:
强制转化列表为集合类型:list(set(l))
法二:
将列表转化为字典,拿出里面的所有key值: dict.fromkeys(l).keys()
In [6]: l=[1,2,3,1,2,3]
In [7]: type(l)
Out[7]: list
In [8]: dict.fromkeys(l) ##直接使用内置函数会默认将value设置为None
Out[8]: {
1: None, 2: None, 3: None}
In [9]: dict.fromkeys(l).keys() ##加上.keys()的方法则只显示keys
Out[9]: [1, 2, 3]
2.实现switch的两种方法:
法一:
通过:if...elif...else...的判断语句实现
举例:
#!/usr/bin/env python
#coding:utf-8
"""
file:ifelifelse.py
date:9/4/178:00 PM
author:hx
desc:if...elif...else...实现switch语句
"""
def add(x,y):
return x+y
def cut(x,y):
return x-y
def multi(x,y):
return x*y
def div(x,y):
return x/y
x=input("x:")
oper=raw_input("oper:")
y=input("y:")
if oper=="+":
print add(x,y)
elif oper=="-":
print cut(x,y)
elif oper=="*":
print multi(x,y)
elif oper=="/":
print div(x,y)
else:
print "error"
运行结果:
/usr/bin/python2.7 /root/PycharmProjects/PyProjectV3/ifelifelse.py
x:1
oper:+
y:2
3
Process finished with exit code 0
法二:
通过字典实现: d= {
"key":func}
举例:
In [11]: from __future__ import division
In [12]: def add(x,y):
....: return x+y
....:
In [13]: def cut(x,y):
return x-y
....:
In [14]: def multi(x,y):
return x*y
....:
In [15]: def div(x,y):
return x/y
....:
In [16]: oper={
'+':add,'-':cut,'*':multi,'/':div}
In [17]: print op
open oper
In [17]: print oper['+'](1,2)
3
In [18]: print add(1,2)
3
一、集合
1. 集合的引入:
概念:集合具有交,并,差和对等差分的运算,可以解决生活中的许多实际问题