-1

I need to iterate sequentially until I find or maximize. For example:

ds = [1,2,3,4,5,6,7,8,9]
tmp = 3 # start (variable)
max = 5 # maximize (variable)
target = 8

so output: [4,5,6,7,8] Sorry, my english is not good.

zckun
  • 3
  • 2
  • So you want to print them or put them in another list? – moe asal Sep 15 '19 at 14:47
  • What have you tried? Do you have any code you can show us with your attempts? What are you getting and what did you expect instead? I'm also a bit confused about what you're trying to do. What does "the last 6 items starting from 5" mean? Starting from index 5? Or starting after the value 5? And you seem to be wrapping around, which is not normal behavior for lists. – Blckknght Sep 15 '19 at 14:49
  • 1
    This seems to be a duplicate of [this](https://stackoverflow.com/questions/8951020/pythonic-circular-list). What you are looking for is a circular list. Please attempt the implementation and edit your question for specifics if you run into any problems. – Enthus3d Sep 15 '19 at 14:52
  • Possible duplicate of [Efficient way to rotate a list in python](https://stackoverflow.com/questions/2150108/efficient-way-to-rotate-a-list-in-python) – Anoop R Desai Sep 15 '19 at 15:06

4 Answers4

1

As a very simple approach you could index over the concatenation with the same list.
However, from memory point of view certainly not the best solution.

# ds = [1,2,3,4,5,6,7,8,9] 

start = 4
length = 7

res = (ds + ds)[start:start+length]
# [5, 6, 7, 8, 9, 1, 2]
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
0

Try:

>>> ds
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> (ds * ((5+6)//len(ds) + 1))[5:5+6]
[6, 7, 8, 9, 1, 2]

Now - 5 is your starting position 5+6 is your end position. You want to iterate over whole data set, as many times to contain end position, so: ((5+6)//len(ds) + 1)

In case if your starting position would be in second, or later repetition (so in your case if it would > 8. You can move it back by subtracting: (startPosition//len(ds)) * len(ds) from both start position, and end position.

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
  • This doesn't match the questioner's desired output. It's not really clear what they are actually asking for, but I doubt this is it. – Blckknght Sep 15 '19 at 14:50
  • I need to iterate sequentially until I find or maximize. For example: ds = [1,2,3,4,5,6,7,8,9] tmp = 3 # start (variable) max = 5 # maximize (variable) target = 8 so output: [4,5,6,7,8] Sorry, my english is not good. – zckun Sep 15 '19 at 15:19
  • @zckun yup, try now - it's more generalized approach – Grzegorz Skibinski Sep 15 '19 at 16:11
0

There is a built-in way to do this.

new_data = i[starting_index : ending_index]

You can leave a number blank if you want to get the rest of the list. Like:

>>>i = [0,8,9,4]
>>>i[1:]
[8,9,4]
moe asal
  • 750
  • 8
  • 24
0

see this solution i used a for loop to reach your target

ds = [1,2,3,4,5,6,7,8,9]
tmp = 3 # start (variable)
max = 5 # maximize (variable)
target=8
i=0  # itiration to loop on the list
for x in ds: 
    if ds[i]<tmp: #till now we didnt reach start point
        i=i+1
    else:
        print(ds[i]) 
        i=i+1
        if  i == target: #since the target has been reached 
            break
Sara Nabil
  • 60
  • 4