Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:45:24+00:00 2026-05-18T20:45:24+00:00

I’m quite new to programming so I’m sure there’s a terser way to pose

  • 0

I’m quite new to programming so I’m sure there’s a terser way to pose this, but I’m trying to create a personal bookmarking program. Given multiple urls each with a list of tags ordered by relevance, I want to be able to create a search consisting of a list of tags that returns a list of most relevant urls. My first solution, below, is to give the first tag a value of 1, the second 2, and so on & let the python list sort function do the rest. 2 questions:

1) Is there a much more elegant/efficient way of doing this (embarrass me!)
2) Any other general approaches to the sorting by relevance given the inputs above problem?

Much obliged.

# 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:45:24+00:00Added an answer on May 18, 2026 at 8:45 pm

    1) Is there a much more elegant/efficient way of doing this (embarrass me!)

    Sure thing. The basic idea: quit trying to tell Python what to do, and just ask it for what you want.

    content = [
        {'url':'url1', 'taglist':['b','a','c','d']},
        {'url':'url2', 'taglist':['c','a','b','d']},
        {'url':'url3', 'taglist':['a','b','c','d']},
        {'url':'url4', 'taglist':['a','b','d','c']},
        {'url':'url5', 'taglist':['d','a','c','b']}
    ]
    
    search = {'d' : 1, 'a' : 2, 'b' : 3}
    
    # We can create the tag cloud like this:
    # tagCloud = set(sum((note['taglist'] for note in content), []))
    # But we don't actually need it: instead, we'll just use a default value
    # when looking things up in the 'search' dict.
    
    # Create a [[relevance, tag],[],[],...] result list & sort 
    result = sorted(
        [
            [search.get(tag, 100), tag]
            for tag in note['taglist']
        ] + [[note['url']]]
        # The result will look like [ [relevance, tag],... , [url] ]
        # Note that the url is wrapped in a list too. This makes the
        # last processing step easier: we just take the last element of
        # each nested list.
        for note in content
    )
    
    # Remove the relevance values & recreate a list containing
    # the url string followed by corresponding tags. 
    finalResult = [
        [x[-1] for x in note]
        for note in result
    ]
    
    print "Content: ", content
    print "Search: ", search
    print "Final Result: ", finalResult
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.