[Tutor] List and dictionary comprehensions

Armindo Rodrigues armindorod at gmail.com
Sun Sep 28 04:36:12 CEST 2014


Hi everyone,

This is my first post so I don't know if I am asking the correct way so let
me know if I messed anything up.

***Please note. My code contains a list of quotes that has many lines. I
have noted the beginning and end of the quotes list so you can easily skip
and go straight to the code section. ***


This is technically NOT a homework assignment. I am the teaching assistant
for the Python course at my school. The teacher doesn't give me the
homework ahead of time so I typically try the homework myself so I can help
anyone else out that may be confused. The assignment has come and gone but
I want to challenge myself with making this thing more efficient and learn
comprehensions along the way.

The assignment was as follows:
The teacher provided the class with a list of quotes called data_list. We
were to implement a very simple search algorithm that took in a user's
query and we searched based on those words. If the user entered an OR then
we searched for quotes that contained either of the words. Any other
combination of AND OR will be an AND search.

Once we completed the search algorithm, the assignment called for
pre-processing that would make the search more efficient and faster. I
created a dictionary based on each word in the quotes list as the key and
then searched against those words. I greatly increased the search time.

MY QUESTION:
Can anyone look at this and explain how I could create a list comprehension
and a dictionary comprehension if possible? Also any suggestions with
making the search faster would be appreciated.


import re
from datetime import datetime
import time


###################  DATA LIST STARTS HERE

data_list=["And now here is my secret, a very simple secret: It is only
with the heart that one can see rightly; what is essential is invisible to
the eye.",
"All grown-ups were once children... but only few of them remember it.",
"People have forgotten this truth,\" the fox said. \"But you mustn?t forget
it. You become responsible forever for what you?ve tamed. You?re
responsible for your rose.",
"It is the time you have wasted for your rose that makes your rose so
important.",
"The most beautiful things in the world cannot be seen or touched, they are
felt with the heart.",
"What makes the desert beautiful,' said the little prince, 'is that
somewhere it hides a well...",
"You - you alone will have the stars as no one else has them...In one of
the stars I shall be living. In one of them I shall be laughing. And so it
will be as if all the stars were laughing, when you look at the sky at
night...You - only you - will have stars that can laugh.",
"Well, I must endure the presence of a few caterpillars if I wish to become
acquainted with the butterflies.",
"You see, one loves the sunset when one is so sad.",
"You're beautiful, but you're empty...One couldn't die for you. Of course,
an ordinary passerby would think my rose looked just like you. But my rose,
all on her own, is more important than all of you together, since she's the
one I've watered. Since she's the one I put under glass, since she's the
one I sheltered behind the screen. Since she's the one for whom I killed
the caterpillars (except the two or three butterflies). Since she's the one
I listened to when she complained, or when she boasted, or even sometimes
when she said nothing at all. Since she's my rose.",
"If you love a flower that lives on a star, it is sweet to look at the sky
at night. All the stars are a-bloom with flowers...",
"And when your sorrow is comforted (time soothes all sorrows) you will be
content that you have known me. You will always be my friend. You will want
to laugh with me. And you will sometimes open your window, so, for that
pleasure . . . And your friends will be properly astonished to see you
laughing as you look up at the sky! Then you will say to them, 'Yes, the
stars always make me laugh!' And they will think you are crazy. It will be
a very shabby trick that I shall have played on you...",
"You become responsible, forever, for what you have tamed.",
"Of course I?ll hurt you. Of course you?ll hurt me. Of course we will hurt
each other. But this is the very condition of existence. To become spring,
means accepting the risk of winter. To become presence, means accepting the
risk of absence.",
"Where are the people?\" resumed the little prince at last. \"It?s a little
lonely in the desert\" \"It is lonely when you?re among people, too,\" said
the snake.",
"All men have stars, but they are not the same things for different people.
For some, who are travelers, the stars are guides. For others they are no
more than little lights in the sky. For others, who are scholars, they are
problems... But all these stars are silent. You-You alone will have stars
as no one else has them... In one of the stars I shall be living. In one of
them I shall be laughing. And so it will be as if all the stars will be
laughing when you look at the sky at night..You, only you, will have stars
that can laugh! And when your sorrow is comforted (time soothes all
sorrows) you will be content that you have known me... You will always be
my friend. You will want to laugh with me. And you will sometimes open your
window, so, for that pleasure... It will be as if, in place of the stars, I
had given you a great number of little bells that knew how to laugh",
"She cast her fragrance and her radiance over me. I ought never to have run
away from her... I ought to have guessed all the affection that lay behind
her poor little stratagems. Flowers are so inconsistent! But I was too
young to know how to love her...",
"A rock pile ceases to be a rock pile the moment a single man contemplates
it, bearing within him the image of a cathedral.",
"I did not know how to reach him, how to catch up with him... The land of
tears is so mysterious.",
"I remembered the fox. One runs the risk of crying a bit if one allows
oneself to be tamed.",
"When someone blushes, doesn't that mean 'yes'?",
"You're beautiful, but you're empty. No one could die for you.",
"In those days, I didn't understand anything. I should have judged her
according to her actions, not her words. She perfumed my planet and lit up
my life. I should never have run away! I ought to have realized the
tenderness underlying her silly pretensions. Flowers are so contradictory!
But I was too young to know how to love her.",
"I have lived a great deal among grown-ups. I have seen them intimately,
close at hand. And that hasn?t much improved my opinion of them.",
"Grown-ups love figures... When you tell them you've made a new friend they
never ask you any questions about essential matters. They never say to you
\"What does his voice sound like? What games does he love best? Does he
collect butterflies? \" Instead they demand \"How old is he? How much does
he weigh? How much money does his father make? \" Only from these figures
do they think they have learned anything about him.",
"The proof that the little prince existed is that he was charming, that he
laughed, and that he was looking for a sheep. If anybody wants a sheep,
that is a proof that he exists.",
"No one is ever satisfied where he is.",
"I was too young to know how to love her.",
"I am who I am and I have the need to be.",
"But the conceited man did not hear him. Conceited people never hear
anything but praise.",
"Men have no more time to understand anything. They buy things all ready
made at the shops. But there is no shop anywhere where one can buy
friendship, and so men have no friends any more. If you want a friend, tame
me...",
"For millions of years flowers have been producing thorns. For millions of
years sheep have been eating them all the same. And it's not serious,
trying to understand why flowers go to such trouble to produce thorns that
are good for nothing? It's not important, the war between the sheep and the
flowers? It's no more serious and more important than the numbers that fat
red gentleman is adding up? Suppose I happen to know a unique flower, one
that exists nowhere in the world except on my planet, one that a little
sheep can wipe out in a single bite one morning, just like that, without
even realizing what he'd doing - that isn't important? If someone loves a
flower of which just one example exists among all the millions and millions
of stars, that's enough to make him happy when he looks at the stars. He
tells himself 'My flower's up there somewhere...' But if the sheep eats the
flower, then for him it's as if, suddenly, all the stars went out. And that
isn't important?",
"I have always loved the desert. One sits down on a desert sand dune, sees
nothing, hears nothing. Yet through the silence something throbs, and
gleams...",
"People have stars, but they aren't the same. For travelers, the stars are
guides. For other people, they're nothing but tiny lights. And for still
others, for scholars, they're problems... But all those stars are silent
stars. You, though, you'll have stars like nobody else... since I'll be
laughing on one of them, for you it'll be as if all the stars are laughing.
You'll have stars that can laugh!... and it'll be as if I had given you,
instead of stars, a lot of tiny bells that know how to laugh ...",
"What does tamed mean? It's something that's been too often neglected. It
means to create ties.",
"The only things you learn are the things you tame",
"One runs the risk of weeping a little, if one lets himself be tamed.",
"You're not at all like my rose. You're nothing at all yet. No one has
tamed you and you haven't tamed anyone. You're the way my fox was. He was
just a fox like a hundred thousand others. But I've made him my own and now
he is unique in the world.",
"I ought not to have listened to her,' he confided to me one day. 'One
never ought to listen to the flowers. One should simply look at them and
breathe their fragrance. Mine perfumed all my planet. But I did not know
how to take pleasure in all her grace.",
"You will have five hundred million little bells, and I shall have five
hundred million springs of fresh water...",
"if you tame me, then we shall need each other. To me, you will be unique
in all the world. To you, I shall be unique in all the world if you tame
me, it will be as if the sun came to shine on my life. I shall know the
sound of a step that will be different from all the others. Other steps
send me hurrying back underneath the ground. Yours will call me, like
music, out of my burrow", "If you tame me, it would be as if the sun came
to shine on my life.",
"Men have no more time to understand anything. They buy ready-made things
in the shops. But since there are no shops where you can buy friends, men
no longer have any friends.",
"All grown-ups were children first. (But few remember it).",
"Of course, I love you,' the flower said to him. 'If you were not aware of
it, it was my fault.",
"The thing that is important is the thing that is not seen.",
"It is much more difficult to judge oneself than to judge others.",
"You know...my flower...I'm responsible for her. And she's so weak! And so
naive. She has four ridiculous thorns to defend her against the world...",
"\"Ephemeral\" It means 'which is in danger of speedy disappearance.",
"He fell as gently as a tree falls. There was not even any sound..",
"The house, the stars, the desert -- what gives them their beauty is
something that is invisible!",
"\"My life is very monotonous,\" the fox said. \"I hunt chickens; men hunt
me. All the chickens are just alike, and all the men are just alike. And,
in consequence, I am a little bored.\"",
"Sometimes, there is no harm in putting off a piece of work until another
day.",
"Only the children know what they are looking for.",
"Grown ups never understood anything by themselves. And it is rather
tedious to have to explain things to them time and again",
"It is much more difficult to judge oneself than to judge others. If you
succeed in judging yourself rightly, then you are a man of true wisdom.",
"Words are the source of misunderstandings.",
"And the little prince broke into a lovely peal of laughter, which
irritated me very much. I like my misfortunes to be taken seriously.",
"\"What a queer planet!\" he thought. \"It is altogether dry, and
altogether pointed, and altogether harsh and forbidding. And the people
have no imagination. They repeat whatever one says to them . . . On my
planet I had a flower; she always was the first to speak . . .",
"I know a planet where there is a certain red-faced gentleman. He has never
smelled a flower. He has never looked at a star. He has never loved any
one. He has never done anything in his life but add up figures. And all day
he says over and over, just like you: 'I am busy with matters of
consequence!' And that makes him swell up with pride. But he is not a man -
he is a mushroom!",
"But if you tame me, my life will be filled with sunshine. I'll know the
sound of footsteps that will be different from all the rest. Others send me
back underground. Yours will call me out of my burrow like music.",
"I believe that for his escape he took advantage of the migration of a
flock of wild birds.",
"He had taken seriously words which were without importance, and it made
him very unhappy.",
"If you come at four in the afternoon, I'll begin to be happy by three.",
"One must require from each one the duty which each one can perform.
Accepted authority rests first of all on reason.",
"Straight ahead you can't go very far.",
"One sees clearly only with the heart. Anything essential is invisible to
the eyes.",
"I wonder if the stars are lit up so that each one of us can find her own
star again.",
"My drawing was not a picture of a hat. It was a picture of a boa
constrictor digesting an elephant.",
"To you I am just a fox, like a hundred thousand others, but if you tame me
we shall need one another and I shall be unique to you in all the world.",
"Don't linger like this. You have decided to go away. Now go!",
"But I, alas, do not know how to see sheep through the walls of boxes.
Perhaps I am a little like the grown-ups. I have had to grow old.",
"I remembered the fox. You risk tears if you let yourself be tamed.",
"Wait for a time, exactly under the star. Then, if a little man appears who
laughs, who has golden hair and refuses to answer questions, you will know
who he is, If this should happen, please comfort me. Send me word that he
has come back.",
"If I am attempting to describe him, it is in order not to forget him. It
is sad to forget a friend. Not every one has had a friend.",
"The thing that is important is the thing that is not seen",
"Nevertheless he is the only one of them all who does not seem to me
ridiculous. Perhaps that is because he is thinking of something else
besides himself.",
"If someone loves a flower of which just one exists among all the millions
and millions of stars, that's enough to make him happy when he looks at the
stars.",
"If you want to build a ship, don't drum up people to collect wood and
don't assign them tasks and work, but rather teach them to long for the
endless immensity of the sea.",
"Will you draw me a sheep?",
"It's good to have a friend. Even if you're going to die.",
"If you love a flower that lives on a star, then it's good at night, to
look up at the sky. All the stars are blossoming."]


################## CODE STARTS HERE

#Create a list of words taken from each individual word in the datalist
word_list = []
for item in data_list:
    for word in item.split(" "):
        word = re.sub('^[^a-zA-z]*|[^a-zA-Z]*$','', word) #Remove
punctuation
        word_list.append(word)
word_list = sorted(list(set(word_list))) #Remove repeated words

quotesDict = {}
for word in word_list:
    quotesDict.setdefault(word,[]) #Create a dictionary with keys based on
each word in the word list

for key, value in quotesDict.items():
    indexofquote = 0
    for quote in data_list:
        if key in quote:
            quotesDict[key].append(indexofquote) #Append the index of the
found quotes to the dictionary key
        indexofquote+=1

query=input("query: ")
query = query.strip(" ").split(" ")
query = list(set(query))

start_time = time.time()

FoundQuotes = []

# Right now the OR search just prints out the index of the found quotes.
if ("or" in query) and ("and" not in query):
    query.remove("or")
    print("Performing OR search for: ", query)
    for item in query:
        if (item in quotesDict):
            print("FOUND ",len(quotesDict[item]),  " ", item, "QUOTES: ",
quotesDict.get(item))
    print("\n--- Execution ---\n", (time.time() - start_time) * 1000,
"microseconds\n")

else:
    if "and" in query:
        query.remove("and")
    if "or" in query:
        query.remove("or")
    print("Performing AND search for: ", query)

    for item in query:
        if (item in quotesDict):
            FoundQuotes = FoundQuotes + (quotesDict.get(item))
    FoundQuotes = list(set([x for x in FoundQuotes if FoundQuotes.count(x)
> 1]))
    for x in FoundQuotes:
        print(data_list[x])
    print("\n--- Execution ---\n", (time.time() - start_time) * 1000,
"microseconds\n")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140927/67da7bd3/attachment-0001.html>


More information about the Tutor mailing list