2016-03-28 15 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

ありがとうございました。これはまさに私が見ていたものでした。 –

関連する問題