Python程序设计和数据分析#程序1
import random
import sys
def guess(s):
try:
write_in =int(input(s))
if write_in >0:
dig = write_in
else:
print('请重新输⼊⼀个⼤于0的正整数:')
return guess(s)
return dig
except ValueError:
print("请输⼊数字!")
return guess(s)
def catchme(n, m):
list1 =[]
list2 =[-1,1]
for i in range(n):
list1.append(i +1)
t =1
rabbit_in =int(random.random()* n +1)
while t <= m:
guess_in = guess("这次指定⼏(1~{})号洞:".format(n))
if rabbit_in == guess_in:
print("恭喜抓到兔⼦了!")
elif guess_in not in list1:
print("没有该洞⼝,请重新输⼊⼀次")
continue
elif t == m:
print("次数⽤完了,很遗憾没抓到兔⼦!")
else:
print("第{}次机会⽤过了!".format(t))
php文件查看手机t +=1
if rabbit_in ==1:
rabbit_in +=1
elif rabbit_in == n:
rabbit_in -=1
else:
rabbit_in = rabbit_in + list2[int(random.random()*2)]
if __name__ =='__main__':
python数据分析基础教程答案holes = guess("请输⼊洞⼝的数量:")
times = guess("请输⼊玩家猜测的次数:")
catchme(holes, times)
#程序2:
import random
rewardDict ={
'⼀等奖':(0,10),网页制作教程视频免费
'⼆等奖':(10,120),
'三等奖':(120,360)
}
def rewardFun():
"""⽤户得奖等级"""
"""⽤户得奖等级"""
#⽣成⼀个0~1之间的随机数
num = random.random()*360
#判断随机转盘转的是⼏等奖
for k,v in rewardDict.items():
if v[0]<= num < v[1]:
return k
resultDict ={}
for i in range(10000):
res = rewardFun()
if res not in resultDict:
resultDict[res]=1
下载app视频播放器else:
resultDict[res]+=1
for k,v in resultDict.items():
print(k,'------>',v)
#程序3:
import sys
try:
num_in =input("请输⼊裁判⼈数:")
num =int(''.join(i for i in num_in if i in'0123456789'))
except ValueError:
print("请输⼊数字!")
else:
list1 =[]
for i in range(num):
try:
m_in =input("请输⼊第{}个裁判的评分:".format(i +1))
m =int(''.join(i for i in m_in if i in'0123456789'))
list1.append(m)
except ValueError:
print("请输⼊数字!")
n =sum(list1)/float(len(list1))
print("{:.2f}".format(n))
#分析1:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
ad_csv('C://Users/qiqi/Desktop/transcount.csv')
upby('year').an)
二进制数1100等于十六进制数多少ad_csv('C://Users/qiqi/Desktop/gpu_transcount.csv')
upby('year').an)
(df,gpu,how="outer",left_index=True,right_index=True)
place(np.nan,0)
years=df.index.values
counts=df['trans_count'].values
gpu_count=df['gpu_trans_count'].values
cnt_log=np.log(counts)
plt.scatter(years,cnt_log,c=200*years,s=20+200*gpu_count/max(gpu_count), alpha=0.5,label='Scatter Plot')
gpu_start=gpu.index.values.min()
y_ann=np.log(df.at[gpu_start,'trans_count'])
y_ann=np.log(df.at[gpu_start,'trans_count'])
ann_str='First GPU\n %d'%gpu_start
plt.annotate(ann_str,xy=(gpu_start,y_ann),
arrowprops=dict(arrowstyle='->'),
xytext=(-30,+70),textcoords='offset points')
plt.legend(loc=2)
plt.xlabel('year')
plt.ylabel('Log Transistor Counts')
plt.title("Moore's Law & Transistor Counts",fontsize=16)
plt.show()
#分析2:
import pandas as pd
源码熊path1 ="chipotle.tsv"# chipotle.tsv
chipo = pd.read_csv(path1, sep ='\t')
chipo.head(10)
print(chipo.shape[1])
item = chipo[['item_name','quantity']].groupby(['item_name'],as_index=False).agg({'quantity':sum}) item.sort_values(['quantity'],ascending=False,inplace=True)
item.head()
chipo['item_name'].nunique()
chipo['quantity'].sum()
fudian =lambda x:float(x[1:-1])
chipo['item_price']= chipo['item_price'].apply(fudian)
chipo
chipo['revenue']=round(chipo['item_price']* chipo['quantity'],2)
chipo['revenue'].sum()
chipo['order_id'].nunique()
chipo[['order_id','revenue']].groupby(by=['order_id']
).agg({'revenue':sum})['revenue'].mean()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论