You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=[1,2,3,4], and want to loop through it, Python offers the
next option
>>>for i in a: print i
1
2
3
4
I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
writing.
>>for i in range(len(a)): print a[i], i
1 0
2 1
3 2
4 3
I think that it would be great if the Python language, which is a
totaly OOP language, could also use the index variable from the first
example and consider it as an object with a Object variable. I mean
the following.
>>>for i in a: print i, i.index # i.index is my sugesstion
1 0
2 1
3 2
4 3
I think that this would be great and we cou pass by this annoying
"range(len( a))" functions 2 1130
azrael wrote:
You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=[1,2,3,4], and want to loop through it, Python offers the
next option
>>>>for i in a: print i
1
2
3
4
I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
writing.
>>>for i in range(len(a)): print a[i], i
1 0
2 1
3 2
4 3
I think that it would be great if the Python language, which is a
totaly OOP language, could also use the index variable from the first
example and consider it as an object with a Object variable. I mean
the following.
>>>>for i in a: print i, i.index # i.index is my sugesstion
1 0
2 1
3 2
4 3
I think that this would be great and we cou pass by this annoying
"range(len( a))" functions
Luckily for those who read either the documentation or this form regulary,
the solution is already there (since python2.0 or so...)
for i, item in enumerate(itera ble):
...
does the trick.
Diez
azrael a écrit :
You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=[1,2,3,4], and want to loop through it, Python offers the
next option
>>>for i in a: print i
1
2
3
4
I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
writing.
>>>for i in range(len(a)): print a[i], i
1 0
2 1
3 2
4 3
for index, obj in enumerate(any_s equence):
print "obj %s is at position %s" % (obj, index)
print "And I might read the FineManual(tm) some day..."
HTH !-)
Oh, and while we're at it:
I think that it would be great if the Python language, which is a
totaly OOP language, could also use the index variable from the first
example and consider it as an object with a Object variable. I mean
the following.
>>>>>for i in a: print i, i.index # i.index is my sugesstion
Python is 100% OO in that everything (at least everything you can bind
to a name) is an object. But it doesn't mean everything has to be done
using the dotted syntax... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Brian van den Broek |
last post by:
Hi all,
There have been a few posts over the last month or so expressing a bit
of exasperation with the "rising tide of newbie's". (Or, more
accurately, the rising tide of questions from newbie's not trying to
follow ESR's advice.)
A month or so ago (in a thread found here: http://tinyurl.com/5bj8j), I
suggested that it might help the...
|
by: Stewart Midwinter |
last post by:
I'm starting up a new website for a small community of local sports
enthusiasts. I want to be able to create pages of information easily
(i.e. a content management system), but also allow for discussion on
multiple topics (i.e. a forum). Numerous people should be able to log
in and create or update content, under the guidance of an admin...
|
by: Yoni Rapoport |
last post by:
Hy everyone, just wanted to share a thought regarding UI.
If you really think about it, events such as "Click", "DoubleClick",
"KeyDown", "KeyUp", etc... (which clutter every winforms control we see) are
mostly annoying and sometimes even harmful.
How does knowing a control was clicked by the mouse (and how many times it
was clicked) help a...
|
by: Andrew Ducker |
last post by:
Almost every project I work on, I end up with some variant of an
IResettable interface - so that controls can be reset to a default
value. Controls have a ResetText method, but that's not much use for
checkboxes, and you can't set a default for things to be reset back to,
on a control by control basis.
Does anyone else find themselves...
|
by: |
last post by:
I need source control to serve just one developer: me. Can someone suggest a
system that would suit my needs? I am the only person writing code, and I
manage the servers. Many of the SC solutions out there seem premised around
the needs of a large team and coordinating their work. That's not me.
Data points that might help: I'm on MSDN...
| |
by: Bruce One |
last post by:
Well folks, considering the high prices for the current VSTS MSDN Premium
subscription, I propose we take a pause and look of what each one of the 3
possible package DOES NOT have:
Team Edition for Software Architects: Unit Testing, Code Coverage / Dynamic
Code Analyzer, Static Code Analyzer, Code Profiler / Load Testing, Manual
Testing,...
|
by: ADezii |
last post by:
If you want to visit each item in an Array, you have two alternatives:
Use a For Each..Next loop, using a Variant to retrieve each value in turn.
Use a For...Next loop, looping from the Lower Bound to the Upper Bound of the Array.
For Each...Next seems simpler because you need not worry about retrieving the Lower and Upper Bounds- the...
|
by: Mikhail Kovalev |
last post by:
Hi.
I work with recursive array trees of 10-20 levels in PHP. So far I
have been using serialize() to store the arrays, generating files 5+
MB large, which take 10-15 seconds to unserialize and about tenfold of
RAM! I've been adviced to use MySQL (relational db's), but are there
any other options beside that and var_export/include?...
|
by: Jean-Paul Calderone |
last post by:
On Mon, 13 Oct 2008 08:56:34 -0700 (PDT), azrael <jura.grozni@gmail.comwrote:
Adding a random new attribute to arbitrary objects any time they happen
to end up in a for loop would be catastrophically terrible. Consider
the enumerate builtin, instead.
.... print i, e
....
0 a
1 b
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |