Python Forum
looking for a sprcil iterator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for a sprcil iterator
#1
i am looking for some kind of object type that can be an iterator, such as in a for loop, and can also have items added to it either on the front (do it would be the next item) or on the back (so it would be the last item) or both. that and i want to be able to do this from within the body of the for loop that is currently iterating it. adding an item would make it so that look cycles longer. anyone know?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
A class can be made to be iterable by implementing the __iter__() and __next__() functions.
The iter function creates the placeholder within the object and returns itself.
The next function gets the current data, advances the placeholder to the next data, and returns the current data.
With these two functions implemented in a class then you can use a for loop to iterate over all the items in the class.
What are you trying to store in your class?
Do you have any code so far?
It's often the case that using the existing list or dictionaries is faster than making your own class.
Reply
#3
the code i have so far uses a while loop on a list made from a string, that a uses .pop() on it to get items out and [:0] to push an item back in. it works this way but i'd rather have a for loop on a genuine iterator of some kind. making one means a lot of code to add to this project.

    cntl = [x for x in cstr]
    while cntl:
        ch = cntl.pop(0)
        if ch == endch:
           cntl[:0] = [ch]
           break
       ...
    ...
    while cntl:
        ch = cntl.pop(0)
        ...
in the above example, the 2nd loop sees the character that ended the first loop as its first character. the real code is much larger than this but this is basically what it does
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Jun-13-2019, 12:16 AM)Skaperen Wrote: the code i have so far uses a while loop on a list made from a string, that a uses .pop() on it to get items out and [:0] to push an item back in. it works this way but i'd rather have a for loop on a genuine iterator of some kind. making one means a lot of code to add to this project.

    cntl = [x for x in cstr]
    while cntl:
        ch = cntl.pop(0)
        if ch == endch:
           cntl[:0] = [ch]
           break
       ...
    ...
    while cntl:
        ch = cntl.pop(0)
        ...
in the above example, the 2nd loop sees the character that ended the first loop as its first character. the real code is much larger than this but this is basically what it does

What are you trying to do?
Are you trying to replace characters in a string?
Are you trying to remove characters in a string?
Reply
#5
it is interpreting the string as a (compact) specialized language
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Jun-13-2019, 12:47 AM)Skaperen Wrote: it is interpreting the string as a (compact) specialized language

Ok, but what actions are you trying to take to do so?
Your code sample appears to examine every character in a string and only leave specific values in the string.
If you can provide more details you will probably get a better answer to your question.
Reply
#7
i am not going to post the whole code. i get people responding about all kinds of other things when i do that.

but i can tell you a little more about the program. it is sorta between the Unix cut command and the (g)awk command. it is a tool to specify how to reformat columns of an input file to produce an output file with more sophistication than the cut command but not as much as the (g)awk command. it uses a mini scripting language that is given to it on the command line. i actually have 2 different ideas how to do this language and i plan to try both to compare ease of use and performance.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(Jun-13-2019, 01:36 AM)Skaperen Wrote: i am not going to post the whole code. i get people responding about all kinds of other things when i do that.
I get that, but you should consider posting an algorithm at least if you want specific help.
For a general answer, my first reply tells how to make an iterable class, but I do not believe that you need one and that your problem is much simpler that you've made it so far.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,771 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  resetting an iterator to full Skaperen 7 6,810 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  popping an iterator Skaperen 11 3,600 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  q re glob.iglob iterator and close jimr 2 2,179 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Problem with an iterator grimm1111 9 4,217 Feb-06-2021, 09:22 PM
Last Post: grimm1111
  Multi-class iterator Pedroski55 2 2,336 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  is a str object a valid iterator? Skaperen 6 5,544 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  discard one from an iterator Skaperen 1 1,957 Dec-29-2019, 11:02 PM
Last Post: ichabod801
  how do i pass duplicates in my range iterator? pseudo 3 2,294 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  last pass of for x in iterator: Skaperen 13 5,725 May-20-2019, 10:05 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020