Question on Manipulating List and on Python

Discussion in 'Python' started by Subhabrata Banerjee, Sep 29, 2011.

  1. Dear Group,

    I have two questions one on list manipulation and other on Python.

    (i) I have a file of lists. Now, the first digit starts with a number
    or index, like,

    [001, "Obama", "USA", "President"]
    [002 "Major", "UK", "PM"]
    [003 "Singh", "INDIA", "PM"]

    Initially, I am reading the file and taking as
    for line in file:
    line_word=line.split
    print line_word

    I am getting the above result. But, my problem is if I read the array
    position 0, which is a number then it should give me reciprocal words
    like
    if
    word1=line_word[0]
    if word1==001:
    I should get line_word[1], line_word[2] reciprocating the value.

    Predefining it solves the problem, but I want to capture and do as it
    iterates. If any one can help?

    (ii) My second question is posted by one of my colleagues, what makes
    Python so fast?

    Regards,
    Subhabrata Banerjee.
    Subhabrata Banerjee, Sep 29, 2011
    #1
    1. Advertising

  2. Subhabrata Banerjee

    John Gordon Guest

    In <> Subhabrata Banerjee <> writes:

    > (i) I have a file of lists. Now, the first digit starts with a number
    > or index, like,


    > [001, "Obama", "USA", "President"]
    > [002 "Major", "UK", "PM"]
    > [003 "Singh", "INDIA", "PM"]


    > Initially, I am reading the file and taking as
    > for line in file:
    > line_word=line.split
    > print line_word


    This isn't your actual code. Please show us your real code, along with
    a sample of your input file.

    > (ii) My second question is posted by one of my colleagues, what makes
    > Python so fast?


    Fast compared to what? Why does your colleague believe it should be
    slower?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Sep 29, 2011
    #2
    1. Advertising

  3. Subhabrata Banerjee

    David Guest

    > word1=line_word[0]
    > if word1==001:



    You are comparing a string and an integer assuming you are reading a
    text file.

    integers
    word1=int(line_word[0])
    if word1=1:

    strings
    word1=line_word[0]
    if word1=="001:"
    David, Sep 29, 2011
    #3
  4. On Sep 29, 9:27 pm, John Gordon <> wrote:
    > In <> Subhabrata Banerjee <> writes:
    >
    > > (i) I have a file of lists. Now, the first digit starts with a number
    > > or index, like,
    > > [001, "Obama", "USA", "President"]
    > > [002  "Major", "UK", "PM"]
    > > [003  "Singh", "INDIA", "PM"]
    > > Initially, I am reading the file and taking as
    > > for line in file:
    > >     line_word=line.split
    > >     print line_word

    >
    > This isn't your actual code.  Please show us your real code, along with
    > a sample of your input file.
    >
    > > (ii) My second question is posted by one of my colleagues, what makes
    > > Python so fast?

    >
    > Fast compared to what?  Why does your colleague believe it should be
    > slower?
    >
    > --
    > John Gordon                   A is for Amy, who fell down the stairs
    >              B is for Basil, assaulted by bears
    >                                 -- EdwardGorey, "The Gashlycrumb Tinies"


    Hi John,
    The actual code is till now is:

    def name_debugger(n):
    open_file=open("/python27/name1.txt")
    for line in open_file:
    line_word=line.split()
    #print line_word
    word1=line_word[0]
    print word1


    And Python seems faster than C++/Java. It is indeed. I also experience
    it.

    Regards,
    Subhabrata Banerjee.
    Subhabrata Banerjee, Sep 29, 2011
    #4
  5. Subhabrata Banerjee

    Chris Rebert Guest

    On Thu, Sep 29, 2011 at 9:11 AM, Subhabrata Banerjee
    <> wrote:
    > Dear Group,
    >
    > I have two questions one on list manipulation and other on Python.
    >
    > (i) I have a file of lists. Now, the first digit starts with a number
    > or index, like,
    >
    > [001, "Obama", "USA", "President"]
    > [002  "Major", "UK", "PM"]
    > [003  "Singh", "INDIA", "PM"]
    >
    > Initially, I am reading the file and taking as
    > for line in file:
    >    line_word=line.split
    >    print line_word
    >
    > I am getting the above result. But, my problem is if I read the array
    > position 0, which is a number then it should give me reciprocal words
    > like
    > if
    > word1=line_word[0]
    > if word1==001:
    >    I should get line_word[1], line_word[2] reciprocating the value.
    >
    > Predefining it solves the problem, but I want to capture and do as it
    > iterates. If any one can help?


    I'm not really clear what you mean by "reciprocal", so this is just a
    guess: Perhaps you want list slicing?

    >>> line_word = ["001", "Obama", "USA", "President"]
    >>> print(line_word[1:])

    ['Obama', 'USA', 'President']
    >>>


    Details: http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation

    > (ii) My second question is posted by one of my colleagues, what makes
    > Python so fast?


    CPython is actually relatively slow compared to the primary
    implementations of other languages since Python highly dynamic, and
    interpreted rather than compiled to machine code (normally). There are
    currently some promising efforts (like PyPy) to produce a faster
    implementation however.

    Cheers,
    Chris
    --
    http://rebertia.com
    Chris Rebert, Sep 29, 2011
    #5
  6. Le 29/09/2011 18:27, John Gordon a écrit :
    > In<> Subhabrata Banerjee<> writes:
    >
    >> (i) I have a file of lists. Now, the first digit starts with a number
    >> or index, like,

    >
    >> [001, "Obama", "USA", "President"]
    >> [002 "Major", "UK", "PM"]
    >> [003 "Singh", "INDIA", "PM"]


    What about creating a dictionary ?

    dic = {1:["Obama","USA","President"],2:[etc.]]


    >> (ii) My second question is posted by one of my colleagues, what makes
    >> Python so fast?


    If your colleague is used to program inside Word macros, I guess the
    answer ;) If he is used to program in C, I'm less sure.
    It really depends on the context of the question.

    Laurent
    Laurent Claessens, Sep 29, 2011
    #6
  7. On Fri, Sep 30, 2011 at 2:36 AM, Subhabrata Banerjee
    <> wrote:
    > And Python seems faster than C++/Java. It is indeed. I also experience
    > it.
    >


    Python compared to Java? Difficult to compare. Python to C++?
    Impossible to compare. But performance depends MASSIVELY on
    algorithmic quality; if you code the exact same thing in Python and in
    C++, you would probably find that the C++ one is faster, but chances
    are you're implementing approximately the same thing in two quite
    different ways. Or possibly you're using a slow and inefficient
    library or third-party function.

    I've sometimes written code in one language and directly ported it to
    another, and then run both on the same hardware. With most such
    experiments, CPython generally doesn't perform all that well, and C or
    C++ rocks. But frequently, the C/C++ code is more verbose and more
    error-prone than the Python - because Python's biggest boast is not
    that it's fast, but that it's *fast enough*, while being easy to work
    with. (And every once in a while there's something where I want to use
    a pointer to some other variable, and that's a concept that just plain
    doesn't work in Python. You have references to objects, but you can't
    from one place change another variable, without using some kind of
    mutable object that they both reference.)

    ChrisA
    Chris Angelico, Sep 29, 2011
    #7
  8. Subhabrata Banerjee

    John Gordon Guest

    In <> Subhabrata Banerjee <> writes:

    > Hi John,
    > The actual code is till now is:


    > def name_debugger(n):
    > open_file=3Dopen("/python27/name1.txt")
    > for line in open_file:
    > line_word=3Dline.split()
    > #print line_word
    > word1=3Dline_word[0]
    > print word1


    Can you give us some sample lines from /python27/name1.txt ?

    > And Python seems faster than C++/Java. It is indeed. I also experience
    > it.


    It shouldn't be inherently faster than C++ or Java. If it is, it's
    because the C++ or Java code is doing more work.

    Do you have a sample Python program and a sample C++ or Java program
    to demonstrate the speed difference?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Sep 29, 2011
    #8
  9. -----Original Message-----
    From: python-list-bounces+ramit.prasad= [mailto:python-list-bounces+ramit.prasad=] On Behalf Of Chris Angelico
    Sent: Thursday,September 29, 2011 11:51 AM
    To:
    Subject: Re: Question on Manipulating List and on Python

    On Fri, Sep 30, 2011 at 2:36 AM, Subhabrata Banerjee
    <> wrote:

    > And Python seems faster than C++/Java. It is indeed. I also experience
    > it.
    >


    Python compared to Java? Difficult to compare. Python to C++?
    Impossible to compare. But performance depends MASSIVELY on
    algorithmic quality; if you code the exact same thing in Python and in
    C++, you would probably find that the C++ one is faster, but chances
    are you're implementing approximately the same thing in two quite
    different ways. Or possibly you're using a slow and inefficient
    library or third-party function.

    I've sometimes written code in one language and directly ported it to
    another, and then run both on the same hardware. With most such
    experiments, CPython generally doesn't perform all that well, and C or
    C++ rocks. But frequently, the C/C++ code is more verbose and more
    error-prone than the Python - because Python's biggest boast is not
    that it's fast, but that it's *fast enough*, while being easy to work
    with. (Andevery once in a while there's something where I want to use
    a pointerto some other variable, and that's a concept that just plain
    doesn't work in Python. You have references to objects, but you can't
    from one place change another variable, without using some kind of
    mutable object that they both reference.)
    -----------------------------------------------------------------------


    I think Steven D'Aprano had an excellent post on this a week or so ago (on the tutor list, not this one).

    See: http://mail.python.org/pipermail/tutor/2011-September/085573.html




    Ramit


    Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
    712 Main Street | Houston, TX 77002
    work phone: 713 - 216 - 5423



    ChrisA
    --
    http://mail.python.org/mailman/listinfo/python-list
    This email is confidential and subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege, and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
    Prasad, Ramit, Sep 29, 2011
    #9
  10. On Sep 30, 12:52 am, "Prasad, Ramit" <>
    wrote:
    > -----Original Message-----
    > From: python-list-bounces+ramit.prasad= [mailto:python-list-bounces+ramit.prasad=] On Behalf Of Chris Angelico
    > Sent: Thursday, September 29, 2011 11:51 AM
    > To:
    > Subject: Re: Question on Manipulating List and on Python
    >
    > On Fri, Sep 30, 2011 at 2:36 AM, Subhabrata Banerjee
    > <> wrote:
    > > And Python seems faster than C++/Java. It is indeed. I also experience
    > > it.

    >
    > Python compared to Java? Difficult to compare. Python to C++?
    > Impossible to compare. But performance depends MASSIVELY on
    > algorithmic quality; if you code the exact same thing in Python and in
    > C++, you would probably find that the C++ one is faster, but chances
    > are you're implementing approximately the same thing in two quite
    > different ways. Or possibly you're using a slow and inefficient
    > library or third-party function.
    >
    > I've sometimes written code in one language and directly ported it to
    > another, and then run both on the same hardware. With most such
    > experiments, CPython generally doesn't perform all that well, and C or
    > C++ rocks. But frequently, the C/C++ code is more verbose and more
    > error-prone than the Python - because Python's biggest boast is not
    > that it's fast, but that it's *fast enough*, while being easy to work
    > with. (And every once in a while there's something where I want to use
    > a pointer to some other variable, and that's a concept that just plain
    > doesn't work in Python. You have references to objects, but you can't
    > from one place change another variable, without using some kind of
    > mutable object that they both reference.)
    > -----------------------------------------------------------------------
    >
    > I think Steven D'Aprano had an excellent post on this a week or so ago (on the tutor list, not this one).
    >
    > See:http://mail.python.org/pipermail/tutor/2011-September/085573.html
    >
    > Ramit
    >
    > Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
    > 712 Main Street | Houston, TX 77002
    > work phone: 713 - 216 - 5423
    >
    > ChrisA
    > --http://mail.python.org/mailman/listinfo/python-list
    > This email is confidential and subject to important disclaimers and
    > conditions including on offers for the purchase or sale of
    > securities, accuracy and completeness of information, viruses,
    > confidentiality, legal privilege, and legal entity disclaimers,
    > available athttp://www.jpmorgan.com/pages/disclosures/email.  
    >
    >


    Dear Group,
    Thanks for your suggestions. I'll check if these work. Converting to
    dictionary I was thinking but others I have to test. Without patting
    my own back(I do not write algorithms that good), I can say Python is
    indeed damn fast. A reason many of my friends are shifting fast to
    Python and I was suggested by a reputed person of MIT CogLab to use
    Python instead of C++ and it worked just fabulous for me. To my
    collegue I would find out an answer.
    Regards,
    Subhabrata.
    Subhabrata Banerjee, Sep 30, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Wilson

    manipulating PowerPoint with Python?

    Greg Wilson, Jun 27, 2003, in forum: Python
    Replies:
    1
    Views:
    1,675
    Aurélien Géron
    Jun 27, 2003
  2. Josh
    Replies:
    1
    Views:
    291
    Terry Reedy
    Dec 6, 2003
  3. manipulating linked list

    , Aug 3, 2006, in forum: C Programming
    Replies:
    6
    Views:
    353
    Chris Torek
    Aug 7, 2006
  4. Peng Yu
    Replies:
    5
    Views:
    1,038
    Peng Yu
    Oct 19, 2009
  5. Marc Leconte
    Replies:
    5
    Views:
    256
    Marc Leconte
    Nov 23, 2009
Loading...

Share This Page