Question on Manipulating List and on Python

Chris Rebert clp2 at rebertia.com
Thu Sep 29 18:37:28 CEST 2011


On Thu, Sep 29, 2011 at 9:11 AM, Subhabrata Banerjee
<subhagurgaon2011 at gmail.com> 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



More information about the Python-list mailing list