、[ 'URL'、 'タグ1'、 'TAG2'、..] sおよび検索指定[ 'TAG3'、 'タグ1'、...]の与えられたリストを私は非常にプログラミングに新しいので、私は確信してこれを行うためのより簡単な方法がありますが、私は個人的なブックマークプログラムを作成しようとしています。関連性の高いタグのリストを持つ複数のURLを指定すると、最も関連性の高いURLのリストを返すタグのリストからなる検索を作成できます。私の最初のソリューションは、以下の、最初のタグに1の値が、第二2を与えることであり、そう&上でPythonのリストのソート機能は、残りを行いましょう。 2つの質問:Pythonは質問の並べ替え - 私は、関連するURLリストを返す
1)これを行うには、よりエレガントで効率的な方法がありますか? 2)上記の入力に関連して、関連性によってソートするための他の一般的なアプローチはありますか?
は多くの義務を負います。
# Given a list of saved urls each with a corresponding user-generated taglist
# (ordered by relevance), the user enters a "search" list-of-tags, and is
# returned a sorted list of urls.
# Generate sample "content" linked-list-dictionary. The rationale is to
# be able to add things like 'title' etc at later stages and to
# treat each url/note as in independent entity. But a single dictionary
# approach like "note['url1']=['b','a','c','d']" might work better?
content = []
note = {'url':'url1', 'taglist':['b','a','c','d']}
content.append(note)
note = {'url':'url2', 'taglist':['c','a','b','d']}
content.append(note)
note = {'url':'url3', 'taglist':['a','b','c','d']}
content.append(note)
note = {'url':'url4', 'taglist':['a','b','d','c']}
content.append(note)
note = {'url':'url5', 'taglist':['d','a','c','b']}
content.append(note)
# An example search term of tags, ordered by importance
# I'm using a dictionary with an ordinal number system
# This seems clumsy
search = {'d':1,'a':2,'b':3}
# Create a tagCloud with one entry for each tag that occurs
tagCloud = []
for note in content:
for tag in note['taglist']:
if tagCloud.count(tag) == 0:
tagCloud.append(tag)
# Create a dictionary that associates an integer value denoting
# relevance (1 is most relevant etc) for each existing tag
d={}
for tag in tagCloud:
try:
d[tag]=search[tag]
except KeyError:
d[tag]=100
# Create a [[relevance, tag],[],[],...] result list & sort
result=[]
for note in content:
resultNote=[]
for tag in note['taglist']:
resultNote.append([d[tag],tag])
resultNote.append(note['url'])
result.append(resultNote)
result.sort()
# Remove the relevance values & recreate a list containing
# the url string followed by corresponding tags.
# Its so hacky i've forgotten how it works!
# It's mostly for display, but suggestions on "best-practice"
# intermediate-form data storage?
finalResult=[]
for note in result:
temp=[]
temp.append(note.pop())
for tag in note:
temp.append(tag[1])
finalResult.append(temp)
print "Content: ", content
print "Search: ", search
print "Final Result: ", finalResult
、感謝します。説明のコメントは多くの助けになります。歓声 –
@ David:答えがあなたの要求に従っていれば、それをアップアップして受け入れることは礼儀正しいと考えられます。 – user225312
ahaはい、それは私をupvoteさせませんでした&私は透明な小さなチェックボックスを逃した。 –