JuHui wrote:
> >>> a='String'
> >>> for x in a:
> ... print x
> ...
> S
> t
> r
> i
> n
> g
> >>>
>
> can I get the index number of a in the upon loop within for x in a
> loop?
Although enumerate is the 'right' answer, I personally prefer :
i = 0
while i < len(some_sequence):
val = som
> "Scott" == Scott David Daniels <[EMAIL PROTECTED]> writes:
Scott> I cannot find the distance in meters between Paris and
Scott> London with: for i in range(10): print i
Works for me
def range(x):
yield '332.8 km'
for i in range(10):
print i
...may not be considered b
JuHui wrote:
> which one has best performance?
>
> a:for i in range(0,len(a))
> b:for x in a
> c:for x,y in enumerate(a)
Read up on the timeit module and figure it out for yourself.
The answer will depend on the distribution of your data.
> but, it seems I can't get index number with b format..
thanks a lot!
:)
--
http://mail.python.org/mailman/listinfo/python-list
Em Seg, 2006-04-03 às 08:47 -0700, JuHui escreveu:
> which one has best performance?
Let's see...
> a:for i in range(0,len(a))
$ python2.4 -mtimeit -s 'a=[None]*100' 'for i in range(len(a)):
j = a[i]
'
10 loops, best of 3: 17.7 usec per loop
$ python2.4 -mtimeit -s 'a=[None]*100' 'for i
which one has best performance?
a:for i in range(0,len(a))
b:for x in a
c:for x,y in enumerate(a)
but, it seems I cann't get index number with b format..:(
--
http://mail.python.org/mailman/listinfo/python-list
JuHui wrote:
> >>> a='String'
> >>> for x in a:
> ... print x
> ...
> S
> t
> r
> i
> n
> g
> >>>
>
> can I get the index number of a in the upon loop within for x in a
> loop?
for x, y in enumerate(a)
print x, y
--
http://mail.python.org/mailman/listinfo/python-list
"JuHui" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> >>> a='String'
> >>> for x in a:
> ... print x
> ...
> S
> t
> r
> i
> n
> g
> >>>
>
> can I get the index number of a in the upon loop within for x in a
> loop?
>
Use enumerate. See example below.
-- Paul
>>> a = "Strin
Try this:
>>> a='String'
>>> i=0
>>> for x in a:
... print i, x
... i+=1
...
0 S
1 t
2 r
3 i
4 n
5 g
--
http://mail.python.org/mailman/listinfo/python-list
>>> a='String'
>>> for x in a:
... print x
...
S
t
r
i
n
g
>>>
can I get the index number of a in the upon loop within for x in a
loop?
--
http://mail.python.org/mailman/listinfo/python-list
10 matches
Mail list logo