2016-03-28 183 views
0

所以我從HTML頁面中列出了元素並計算了這些元素的頻率。但我只需要一些特定的元素,如「bb」和「nw」。所以我不知道他們在列表中的位置,我不知道如何將它們與其他元素分開。Python:獲取特定的列表元素

這是我到目前爲止的代碼:

from bs4 import BeautifulSoup 
import urllib2 
import re 
import operator 
from collections import Counter 
from string import punctuation 

source_code = urllib2.urlopen('https://de.wikipedia.org/wiki/Liste_von_Angriffen_auf_Fl%C3%BCchtlinge_und_Fl%C3%BCchtlingsunterk%C3%BCnfte_in_Deutschland/bis_2014') 
html = source_code.read() 
soup = BeautifulSoup(html, "html.parser") 

text = (''.join(s.findAll(text=True))for s in soup.findAll('a')) 

c = Counter((x.rstrip(punctuation).lower() for y in text for x in y.split())) 

bb,nw=operator.itemgetter(1,2)(c.most_common()) 
print(bb,nw) 

謝謝您的幫助和任何提示。

+1

你是什麼意思的,你只需要特定的元素?你的意思是你需要他們的頻率嗎? – Peaceful

回答

2

你可以使用過濾器:

relevant_items = ('bb', 'nw') 
items = filter(lambda x: x[0] in relevant_items, c.most_common()) 

或者,也可以在修真已經過濾:

c = Counter((x.rstrip(punctuation).lower() for y in text for x in y.split() if x in relevant_items)) 
+0

非常感謝。這正是我所期待的vor。 –