Python實戰(zhàn)計劃學(xué)習(xí)筆記(16)第三周作業(yè)

任務(wù)

  1. 各城區(qū)發(fā)帖量Top3大類目和發(fā)帖量
  2. 各大類目中,成色對應(yīng)的平均價格情況

程序1

import pymongo
import charts
from datetime import timedelta,date

client = pymongo.MongoClient('localhost',27017)
test = client['test']
item_info = test['sample']

# In[62]:

for i in item_info.find({},{'_id':0,'pub_date':1}).limit(100):
    print(i)


# In[63]:

#for i in item_info.find():
#    frags = i['pub_date'].split('-')
#    if len(frags)==1:
#        date = frags[0]
#    else:
#        date = '{}.{}.{}'.format(frags[0],frags[1],frags[2])
#    item_info.update_one({'_id':i['_id']},{'$set':{'pub_date':date}})


# In[64]:

def get_all_dates(date1,date2):
    the_date = date(int(date1.split('.')[0]),int(date1.split('.')[1]),int(date1.split('.')[2]))
    end_date = date(int(date2.split('.')[0]),int(date2.split('.')[1]),int(date2.split('.')[2]))
    days = timedelta(days=1)
    while the_date <= end_date:
        yield the_date.strftime('%Y.%m.%d')
        the_date = the_date + days


# In[65]:

#for i in get_all_dates('2015.11.12','2016.01.15'):
#    print(i)


# In[66]:

def get_data_within(date1,data2,areas):
    for area in areas:
        area_day_posts = []
        for date in get_all_dates(date1,data2):
            a = list(item_info.find({'pub_date':date,'area':area}))
            #print('#'*20,date,area,len(a),'#'*20)
            each_day_posts = len(a)
            area_day_posts.append(each_day_posts)
        data = {
            'name':area,
            'data':area_day_posts,
            'type':'line'
        }
        yield data


# In[67]:

for i in get_data_within('2015.11.12','2016.01.15',['朝陽','海淀','西城']):
    print(i)


# In[69]:

options = {
    'chart':{'zoomType':'xy'},
    'title': {'text':'發(fā)帖量統(tǒng)計'},
    'subtitle': {'text':'發(fā)帖量統(tǒng)計'},
    'xAxis' : {'categories':[i for i in get_all_dates('2015.11.12','2016.01.15')]},
    'yAxis' : {'title': { 'text' : '數(shù)量'}}
}

series = [ i for i in get_data_within('2015.11.12','2016.01.15',['朝陽','海淀','西城'])]


# In[70]:

charts.plot(series, show='inline', options = options )

程序1運行結(jié)果

1.jpg

程序2

import pymongo
import charts
from datetime import timedelta,date


# In[317]:

client = pymongo.MongoClient('localhost',27017)
test = client['test']
item_info = test['sample']


# In[318]:

for i in item_info.find({},{'_id':0,'look':1,'price':1,'cates':1}).limit(100):
    print(i)


# In[319]:

def get_look_price_data(the_cates):
    the_price = 0
    look100 = []
    look99 = []
    look95 = []
    look90 = []
    look80 = []
    look70 = []
    the_price_data = []
    for i in item_info.find({},{'_id':0,'look':1,'price':1,'cates':1}):
        if i['cates'][2]== the_cates:
            if i['price'] == '面議':
                the_price = ''
            elif i['price'][-2:]== ' 元':
                the_price = int(i['price'][:-2])
            elif i['price'][-1]=='元':
                the_price = int(i['price'][:-1])
            else:
                the_price = int(i['price'])
        #print(the_price,i['look'])
        if the_price !='' and i['look'] != '-':
            #print(the_price,i['look'])
            if i['look']=='全新':
                look100.append(the_price)
            elif i['look']=='99成新':
                look99.append(the_price)
            elif i['look']=='95成新':
                look95.append(the_price)
            elif i['look']=='9成新':
                look90.append(the_price)
            elif i['look']=='8成新':
                look80.append(the_price)
            elif i['look']=='7成新及以下':
                look70.append(the_price)
    the_price_data = [sum(look100)/len(look100),sum(look99)/len(look99),sum(look95)/len(look95),sum(look90)/len(look90),sum(look80)/len(look80),sum(look70)/len(look70)]
    print(the_price_data)
    data = {
            'name':the_cates,
            'data':the_price_data,
            'type':'line'
        }
    yield data
    


# In[320]:取二手家電類目的成色價格關(guān)系

#get_look_price_data('北京二手家電')


# In[321]:

series = [i for i in get_look_price_data('北京二手臺式機/配件')]
print(series)


# In[322]:

options = {
    'chart':{'zoomType':'xy'},
    'title': {'text':'成色與價格的關(guān)系'},
    'subtitle': {'text':'統(tǒng)計'},
    'xAxis' : {'categories':['全新','99成新','95成新','9成新','8成新','7成新及以下']},
    'yAxis' : {'title': { 'text' : '平均價格'}}
}
charts.plot(series, show='inline',options = options)

程序2運行結(jié)果

2.jpg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末脊奋,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子诚隙,更是在濱河造成了極大的恐慌讶隐,老刑警劉巖久又,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異炉峰,居然都是意外死亡脉执,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門婆廊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巫橄,“玉大人淘邻,你說我怎么就攤上這事湘换。” “怎么了贴浙?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵署恍,是天一觀的道長。 經(jīng)常有香客問我盯质,道長,這世上最難降的妖魔是什么呼巷? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任王悍,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘源譬。我一直安慰自己,他們只是感情好踩娘,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布喉祭。 她就那樣靜靜地躺著,像睡著了一般泛烙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上藐唠,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天孵滞,我揣著相機與錄音鸯匹,去河邊找鬼。 笑死殴蓬,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的染厅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼孤页,長吁一口氣:“原來是場噩夢啊……” “哼涩馆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起魂那,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤涯雅,失蹤者是張志新(化名)和其女友劉穎鲜结,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體拗胜,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡贬养,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了仰美。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片儿礼。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蚊夫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情知纷,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布伍绳,位于F島的核電站乍桂,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏睹酌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一旺芽、第九天 我趴在偏房一處隱蔽的房頂上張望辐啄。 院中可真熱鬧甥绿,春花似錦则披、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽便贵。三九已至,卻和暖如春承璃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背隘梨。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工舷嗡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人进萄。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像可婶,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子矛渴,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容