473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

position in a for loop?

EP
In fear of bringing down the quality of the list, I nevertheless will ask:
How I can tell where I am in a for loop?

I think I am working way too hard to try to determine this - there is
probably a very simple way to know?

Spam_locations=[]
list=['linguine','Spam', 'clams', 'Spam', 'steak', 'onions', 'apples', 'Spam']
for food in list:
if food='Spam':
Spam_location.append(## position in list ##) ## but how do I
back reference the position in the for loop?

I am hoping to get:
Print Spam_locations

[1,3,7]

I was thinking the back reference might be something like:
Spam_location.append(list.__iter__())
But that gives me <listiterator object at 0x00DBE3B0> not a value (position)...

I promise to be smarter next year... any help?
EP

Jul 18 '05 #1
1 1477
you may want to have a look @
http://aspn.activestate.com/ASPN/Coo.../Recipe/204297,
where it is said that 'the interpreter creates a secret
name that only exists while the list is being built.
That name is (usually) "_[1]", and it refers to the bound
method "append" of the list.'. for your specific problem,
the enumerate() solution is probably all that you need;
for other cases (such as checking whether a certain value
is already a member of the list), this solution may come
in handy. for example, in order to get a non-repetitive
string with all the characters found in a given text, you
could say

print ''.join(
[ x
for x in 'my words are repetitive'
if x not in locals()['_[1]'].__self__ ] )

hope that helps,
_wolf
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: b0yce | last post by:
Hi all, I am trying to create a recursive loop transformation that remembers last position of inner loop so that it continues from that point instead of the point from where the recursion...
4
by: Jay | last post by:
Hi, How can I capture mouse position on Image? I found number of script capturing mouse position of the page. But I could not find anything based on image. What I want to find out is X Y...
3
by: akunamatata | last post by:
Hello everyone, I contact this discussiongroup because I encountered a little problem with XSL. Let me explain it: I have following file "position.xml": <?xml version="1.0"?>...
2
by: Bob Cummings | last post by:
Greetings I am stumped. I am trying to create some initial conditions object by reading from a file. These objects have an integer ID and then a list of sub objects. The problem is I will...
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
8
by: Jeff | last post by:
Ok gang, I am going to try and explain my question. I have a page where a person's position is listed. This is what I am using to get that position. I first sort by the round field in the DB,...
24
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
1
by: balderdash | last post by:
Hi I am very close to achieving the output I need but I cant seem to get it right. The problem is I am looping through a table and selecting values, if there are 2 values per (row) Issuer I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.