Question on Manipulating List and on Python

  • Thread starter Subhabrata Banerjee
  • Start date
S

Subhabrata Banerjee

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.
 
J

John Gordon

In said:
(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?
 
D

David

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:"
 
S

Subhabrata Banerjee

In said:
(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?

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.
 
C

Chris Rebert

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
 
L

Laurent Claessens

Le 29/09/2011 18:27, John Gordon a écrit :
In said:
(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.]]


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
 
C

Chris Angelico

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
 
J

John Gordon

In said:
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?
 
P

Prasad, Ramit

-----Original Message-----
From: [email protected] [mailto:p[email protected]] On Behalf Of Chris Angelico
Sent: Thursday,September 29, 2011 11:51 AM
To: (e-mail address removed)
Subject: Re: Question on Manipulating List and on Python

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.
 
S

Subhabrata Banerjee

-----Original Message-----
From: [email protected] [mailto:p[email protected]] On Behalf Of Chris Angelico
Sent: Thursday, September 29, 2011 11:51 AM
To: (e-mail address removed)
Subject: Re: Question on Manipulating List and on Python

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top