第6章-1 输入列表,求列表元素和(eval输入应用)
print(sum(eval(input())))
第6章-2 一帮一
n=int(input())
stu=[]
for i in range(n):
no,name=input().split()
stu.append((no,name))
l=[]
for i in stu[:n//2]:
for j in stu[n//2:][::-1]:
if i[0]!=j[0] and i not in l and j not in l:
l.append(i)
l.append(j)
for i in range(n):
print(l[i][1],end='')
if i%2!=0:
print()
else:
print(' ',end='')
第6章-3 列表或元组的数字元素求和
def f(l):
s=0
if type(l)==type(1):
s+=l
if type(l)==type([]) or type(l)==type((1,)):
for i in l:
s+=f(i)
return s
l=eval(input())
print(f(l))
第6章-4 列表数字元素加权和(1)
def f(l,deep):
s=0
if isinstance(l,int):
s+=l*deep
if isinstance(l,list):
deep+=1
for i in l:
s+=f(i,deep)
return s
l=eval(input())
print(f(l,0))
第6章-5 列表元素个数的加权和(1)
def f(l,deep):
s=0
if isinstance(l,int):
s+=1*deep
if isinstance(l,list):
deep+=1
for i in l:
s+=f(i,deep)
return s
l=eval(input())
print(f(l,0))
第6章-6 求指定层的元素个数
def f(l,deep,num):
s=0
if isinstance(l,int) and deep==num:
s+=1
if isinstance(l,list):
deep+=1
for i in l:
s+=f(i,deep,num)
return s
l=eval(input())
num=int(input())
print(f(l,0,num))
第6章-7 找出总分最高的学生
n=int(input())
stu=[]
for i in range(n):
stu.append(list(input().split()))
stu=sorted(stu,key=lambda x:int(x[2])+int(x[3])+int(x[4]),reverse=True)
print(stu[0][1],stu[0][0],int(stu[0][2])+int(stu[0][3])+int(stu[0][4]))
第6章-8 *6-7 输出全排列
from math import pow
n=int(input())
start=int(pow(10,n-1))
stop=(n+1)*int(pow(10,n-1))
l=set([str(i) for i in range(1,n+1)]) #数字库
for i in range(start,stop):
i=str(i)
s=set()
for j in i:
s.add(j)
if l==s:
print(int(i))