已采纳回答 / liusongsir
因为for循环这里遍历出来的是3个tuple,分别是('Adam', 95),('Lisa', 85),('Bart', 59),然后每个tuple包含两个元素,因为tuple是有序集合,所以可以通过下标访问,下标从0开始,所以x[0]就是'姓名',x[1]就是'分数'。而你说的为什么s[0]不行,这是因为set是无序集合,不能通过下标访问
2015-07-23
def square_of_sum(L):
sum = 0
for key in L:
sum = sum + key * key
return sum
sum = 0
for key in L:
sum = sum + key * key
return sum
2015-07-23
def square_of_sum(L):
sum=0
for x in L:
a=0
a=x*x
sum=sum+a
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
sum=0
for x in L:
a=0
a=x*x
sum=sum+a
return sum
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2015-07-22
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for x in L:
if x in s:
s.remove(x)
else:
s.add(x)
print s
2015-07-22
print [100*a+10*b+c for a in range(1,10) for b in range(0,10) for c in range (0,10) if a==c]
2015-07-22
L = []
for i in range(1,101):
print i
L.append(int(i*i))
print sum(L)
for i in range(1,101):
print i
L.append(int(i*i))
print sum(L)
2015-07-22
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for key in L:
if key in s:
s.remove(key)
else:
s.add(key)
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for key in L:
if key in s:
s.remove(key)
else:
s.add(key)
print s
2015-07-22
for x in range(10)[1:]:
for y in range(10)[1:]:
if(x<y):
print x*10+y
for y in range(10)[1:]:
if(x<y):
print x*10+y
2015-07-22
for x in range(9)[1:]:
for y in range(9)[1:]:
if(x<y):
print x*10+y
for y in range(9)[1:]:
if(x<y):
print x*10+y
2015-07-22
sum = 0
x = 0
while True:
x = x + 1
if x > 100:
break
if x&1==0:
continue
sum = sum+x
print sum
x = 0
while True:
x = x + 1
if x > 100:
break
if x&1==0:
continue
sum = sum+x
print sum
2015-07-22
L = [75, 92, 59, 68]
sum = 0.0
for score in L:
sum += score
print sum / 4
sum = 0.0
for score in L:
sum += score
print sum / 4
2015-07-22