第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從短語列表中查找字典中的短語,并使用找到的短語和計數(shù)創(chuàng)建數(shù)據(jù)框。應(yīng)計算重復(fù)項

如何從短語列表中查找字典中的短語,并使用找到的短語和計數(shù)創(chuàng)建數(shù)據(jù)框。應(yīng)計算重復(fù)項

嚕嚕噠 2022-12-20 16:19:31
phrases = ['i am good', 'going to the market', 'eating cookies']dictionary = {'http://www.firsturl.com': 'i am going to the market and tomorrow will be eating cookies',             'http://www.secondurl.com': 'tomorrow is my birthday and i shall be',              'http://www.thirdurl.com': 'i am good and will go to sleep'}如果至少有一個匹配項:預(yù)期輸出:url                             phrasecount    phrasehttp://www.firsturl.com         2              going to the market, eating cookieshttp://www.thirdurl.com         1              i am good如果所有 3 個 url 都沒有匹配項,則只返回第一次出現(xiàn)的 url,計數(shù)為零,預(yù)期輸出為空白短語:url                            phrasecount    phrasehttp://www.firsturl.com        0     
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經(jīng)驗 獲得超2個贊

df從相應(yīng)的設(shè)置初始數(shù)據(jù)幀dictionary:


df = pd.DataFrame({'urls': list(dictionary.keys()), 'strings': list(dictionary.values())})

pattern = '|'.join(phrases)

處理數(shù)據(jù)幀:


s = df.pop('strings').str.findall(pattern)

df = df.assign(phrasecount=s.str.len(), phrase=s.map(', '.join))

df = df.drop_duplicates(subset='phrasecount') if df['phrasecount'].eq(0).all() else df[df['phrasecount'].ne(0)]

結(jié)果:


# print(df)


                      urls  phrasecount                               phrase

0  http://www.firsturl.com            2  going to the market, eating cookies

2  http://www.thirdurl.com            1                            i am good


查看完整回答
反對 回復(fù) 2022-12-20
  • 1 回答
  • 0 關(guān)注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號