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 6703915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:13:42+00:00 2026-05-26T07:13:42+00:00

While working through the awesome book Programming Collective Intelligence, by Toby Segaran, I’ve encountered

  • 0

While working through the awesome book “Programming Collective Intelligence”, by Toby Segaran, I’ve encountered some techniques in index assignments I’m not entirely familiar with.

Take this for example:

createkey='_'.join(sorted([str(wi) for wi in wordids]))

or:

normalizedscores = dict([(u,float(l)/maxscore) for (u,l) in linkscores.items()])

All the nested tuples in the indexes have me a bit confused. What is actually being assigned to these varibles? I assumed obviously the .join one comes out as a string, but what about the latter? If someone could explain the mechanics of these loops I’d really appreciate it. I assume these are pretty common techniques, but being new to Python, I suppose to ask is a moment’s shame. Thanks!

  • 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-26T07:13:42+00:00Added an answer on May 26, 2026 at 7:13 am
    [str(wi) for wi in wordids]
    

    is a list comprehension.

    a = [str(wi) for wi in wordids]
    

    is the same as

    a = []
    for wi in wordids:
        a.append(str(wi))
    

    So

    createkey='_'.join(sorted([str(wi) for wi in wordids]))
    

    creates a list of strings from each item in wordids, then sorts that list and joins it into a big string using _ as a separator.

    As agf rightly noted, you can also use a generator expression, which looks just like a list comprehension but with parentheses instead of brackets. This avoids construction of a list if you don’t need it later (except for iterating over it). And if you already have parentheses there like in this case with sorted(...) you can simply remove the brackets.

    However, in this special case you won’t be getting a performance benefit (in fact, it’ll be about 10 % slower; I timed it) because sorted() will need to build a list anyway, but it looks a bit nicer:

    createkey='_'.join(sorted(str(wi) for wi in wordids))
    

    normalizedscores = dict([(u,float(l)/maxscore) for (u,l) in linkscores.items()])
    

    iterates through the items of the dictionary linkscores, where each item is a key/value pair. It creates a list of key/l/maxscore tuples and then turns that list back into a dictionary.

    However, since Python 2.7, you could also use dict comprehensions:

    normalizedscores = {u:float(l)/maxscore for (u,l) in linkscores.items()}
    

    Here’s some timing data:

    Python 3.2.2

    >>> import timeit
    >>> timeit.timeit(stmt="a = '_'.join(sorted([str(x) for x in n]))", setup="import random; n = [random.randint(0,1000) for i in range(100)]")
    61.37724242267409
    >>> timeit.timeit(stmt="a = '_'.join(sorted(str(x) for x in n))", setup="import random; n = [random.randint(0,1000) for i in range(100)]")
    66.01814811313774
    

    Python 2.7.2

    >>> import timeit
    >>> timeit.timeit(stmt="a = '_'.join(sorted([str(x) for x in n]))", setup="import random; n = [random.randint(0,1000) for i in range(100)]")
    58.01728623923137
    >>> timeit.timeit(stmt="a = '_'.join(sorted(str(x) for x in n))", setup="import random; n = [random.randint(0,1000) for i in range(100)]")
    60.58927580777687
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working through the Programming Clojure book. While explaining alter and the STM,
I am working through Linux Device Drivers, Third Edition and while the book is
I am working through some Looping exercises, While statements in particular. Here are the
While working my way through the Android tutorials, I came across something I don't
While working through Real World Haskell, I tried to complete the palindrome exercise using
I am working through part4 of Symfony2 , and while updating the controller and
I am working through the excellent The C Programming Language at the moment, and
I came across this code while working through code-analysis warnings on our code base.
I am working through the Introduction to Algorithms book by Cormen, and I have
I'm working through the exercises in the K&R book. Currently I'm stuck at exercise

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.