我想文章对set解析的不是很到位,个人是这样理解的:
set的固定用法是s=set([x1,x2,x3,...]),即x1,x2,x3,...是s集合中不可改变的元素(注意:不能放list类型的元素,因为list内元素可多次赋值),利用for循环遍历时,可依次取出x1,x2,x3,...。本题中set集合是s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)]),很明显set集合中元素X为tuple类型(注:tuple的固定用法是x = ('Adam','Lisa')),该元素X里又有两个元素,即可依次根据索引取出。
set的固定用法是s=set([x1,x2,x3,...]),即x1,x2,x3,...是s集合中不可改变的元素(注意:不能放list类型的元素,因为list内元素可多次赋值),利用for循环遍历时,可依次取出x1,x2,x3,...。本题中set集合是s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)]),很明显set集合中元素X为tuple类型(注:tuple的固定用法是x = ('Adam','Lisa')),该元素X里又有两个元素,即可依次根据索引取出。
2016-01-29
终于看到自己想要的答案了,用Lower转其实 和直接写小写有什么区别。真正我们需要的就是无论输大小它都能匹配上。
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print 'adam'.capitalize() in s
print 'bart'.capitalize() in s
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print 'adam'.capitalize() in s
print 'bart'.capitalize() in s
2016-01-29
import math
def quadratic_equation(a, b, c):
x=0
y=0
if a!=0:
x=(-b+math.sqrt(b*b-4*a*c))/(2*a)
y=(-b-math.sqrt(b*b-4*a*c))/(2*a)
return x,y
else:
return -c/b
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
x=0
y=0
if a!=0:
x=(-b+math.sqrt(b*b-4*a*c))/(2*a)
y=(-b-math.sqrt(b*b-4*a*c))/(2*a)
return x,y
else:
return -c/b
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2016-01-28
def square_of_sum(L):
s = 0
for i in L:
s += i*i
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
s = 0
for i in L:
s += i*i
return s
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2016-01-28