1.列表与元组
#筛选法求素数 4-4.py
primes = [1] * 300;
primes[0:2] = [0,0]
for i in range(2 , 300):
if primes[i]==1:
for j in range (i+1,300):
if primes[j] != 0 and j % i == 0:
primes[j] = 0
print("300以内的素数包括,")
for i in range(2 , 300):
if primes[i]:
print(i , end = "")
#二分查找 4-5.py
ls = [34,64,67,72,73,82,83,85,87,88,90,91,96,98]
x = int(input('请输入待查的数:'))
low = 0
high = len(ls) - 1
while low < high:
mid = (low + high) //2
if ls [mid] < x:
low = mid + 1
elif ls[mid] > x:
high = mid - 1
else:
print('找到{},索引为{}!'.format(x,mid));
break;
else:
print("没有找到{}".format(x))
#模拟掷两个骰子100000次,统计2~12各点数出现的概率 4-7.py
from random import *
seed()
faces = [0]*13
for i in range(100000):
face1 = int(random() * 100) % (6 - 1 + 1) + 1
face2 = int(random() * 100) % (6 - 1 + 1) + 1
faces[face1+face2] += 1
print("模拟掷两个骰子100000次结果如下:")
for i in range(2,13):
rate = faces[i] / 100000
print('点数{}出现了{})次'.format(i,faces[i]),end=",")
print('出现概率{:.2%}'.format(rate))
#武功属性评分统计 4-9.py
attrs =["筋骨","敏捷","气势","反应","技巧","内力"]
tables = [['萧峰',20,17,20,20,18,19],
['杨过',18,19,17,20,18,18],
['令狐冲',12,17,14,20,19,13],
['张无忌',20,17,15,14,20,20],
['郭靖',19,18,19,18,19,20]]
#提取大侠的名字列表names
names = [item[0] for item in tables]
#提取评分列表scores
scores = [item[1:] for item in tables]
#生成各大侠的总分列表totals
totals = [sum(item) for item in scores]
#生成各个属性怕平均分列表avgs
avgs = []
for j in range(6):
avgs.append(sum([scores[i][j] for i in range(5)])/5)
#输出五位大侠的总分
print('\n位大侠的总分是:')
for i in range(5):
print('{:<6}:{:>4}'.format(names[i],totals[i]))
#输出不同属性的平均分
print('\n不同属性的平均分是:')
for i in range(6):
print('{:<8}:{:>4}'.format(attrs[i],avgs[i]))
#输出得分最高的大侠的名字
print('\n总分最高的大侠的名字是:',end='---')
print(names[totals.index(max(totals))])
2.字典与集合
sentence = "Life is short,we need Python."
sentence = sentence.lower()
counts={}
for c in sentence:
if c in counts:
counts[c] = counts[c] + 1
else:
counts[c] = 1
print(counts)
dicAres = {'Russia':1707.5,'Canada':997.1,'China':960.1}
ls = sorted(dicAres)
for country in ls:
print(country,dicAres[country])
dicAres = {'Russia':1707.5,'Canada':997.1,'China':960.1}
lsVK=[(v,k) for k,v in dicAres.items()]
lsVK.sort()
lsVK=[(v,k) for v,k in lsVK]
print(lsVK)
dicAres = {'俄罗斯':1707.5,'加拿大':997.1,'中国':960.1,'美国':936.4,'巴西':854.7}
dicCapitals ={'俄罗斯':'莫斯科','加拿大':'渥太华','中国':'北京','美国':'华盛顿','巴西':'巴西利亚'}
dicCountries={}
for key in dicAres.keys():
dicCountries[key] = [dicAres[key],dicCapitals[key]]
for item in dicCountries.items():
print(item)
setI={'Python','C++','C','Java','C#'}
setT={'Java','C','Python','C++','VB.NET'}
print('IEEE2018排行榜前五的编程语言有:')
print(setI)
print('TIOBE2018排行榜前五的编程语言有:')
print(setT)
print("前五名所有上榜的语言有:")
print(setI | setT)
print('在两个榜单同时进前五的语言有:')
print(setI & setT)
print('只在IEEE进前五的语言有:')
print(setI - setT)
print('只有一个榜单进前五的语言:')
print(setI ^ setT)
#用字典实现武功属性评分统计 5-10.py
tables = {'萧峰':{"筋骨":20,"敏捷":17,"气势":20,"反应":20,"技巧":18,"内力":19},
'杨过':{"筋骨":18,"敏捷":19,"气势":17,"反应":20,"技巧":18,"内力":18},
'令狐冲':{"筋骨":12,"敏捷":17,"气势":14,"反应":20,"技巧":19,"内力":13},
'张无忌':{"筋骨":20,"敏捷":17,"气势":15,"反应":14,"技巧":20,"内力":20},
'郭靖':{"筋骨":19,"敏捷":18,"气势":19,"反应":18,"技巧":19,"内力":20}}
for k,v in tables.items():
tables[k]['总分']=tables[k]['筋骨']+tables[k]['敏捷']+tables[k]['气势']+tables[k]['反应']+tables[k]['技巧']+tables[k]['内力']
print('{:<6}:{:>4}'.format(k,v['总分']))
dictri={}
for k,v in tables.items():
dictri['筋骨']=dictri.get('筋骨',0)+v['筋骨']
dictri['敏捷']=dictri.get('敏捷',0)+v['敏捷']
dictri['气势']=dictri.get('气势',0)+v['气势']
dictri['反应']=dictri.get('反应',0)+v['反应']
dictri['技巧']=dictri.get('技巧',0)+v['技巧']
dictri['内力']=dictri.get('内力',0)+v['内力']
n=len(tables)
for k,v in dictri.items():
dictri[k]=v/n
print("\n不同属性的平均分是:")
for k,v in dictri.items():
print('{:<8}:{:>4}'.format(k,v))
totals=[(v['总分'],k) for k,v in tables.items()]
totals.sort(reverse=True)
print("\n总分最高的大侠是",end='——')
print(totals[0][1])
版权声明:本文为Ethen_CSDN原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。