2

In this code I wanted to iterate over each item in the list starting at index [4] finishing at index [3]. Is there any way to improve the cumbersome manner in which I have written the code to achieve this? I tried "for a in days[4:3] but this only iterated indexes 4 to 6 and never 0-3;

mar = int(input())
days = ["0","1","2","3","4","5","6"]
count = 0
x = "0"
while count < mar:
  for a in days[4],[5],[6],[0],[1],[2],[3]:
    if count < mar:
      count += 1
      x = a   
print(x)
jpp
  • 159,742
  • 34
  • 281
  • 339

4 Answers4

1

itertools islice + cycle

You can use itertools to create an iterator. This is a lazy solution which requires you to call next on the iterator over a specified range.

from itertools import islice, cycle

c = islice(cycle(days), 5, None)

for _ in range(len(days)):
    print(next(c))

5
6
0
1
2
3
4

Only recommended for small iterables:

Note, this member of the toolkit may require significant auxiliary storage (depending on the length of the iterable).

jpp
  • 159,742
  • 34
  • 281
  • 339
  • That's actually pretty nice and lean. Can you guess what 'small' is? I.e. will it perform good bad above 10, 100, 1000, 10000 etc? – sudonym Jun 08 '18 at 15:09
  • @sudonym, Thanks - but lean only for small iterables. Sorry, I haven't tested myself about what small means. I'll do some testing if I have some time. – jpp Jun 08 '18 at 15:10
  • I also have a look myself - this is good for rotating user agents (for example) – sudonym Jun 08 '18 at 15:50
0

try this:

input = '4'                       
start_day = int(input)             
days = ["0","1","2","3","4","5","6"]
days_before = []
# Loop                
for day in days: 
    if int(day) > start_day:      # Either print
        print day    
    else:
        days_before.append(day)   # Or dump to second list
else:                             # When finished
    for day in days_before:       # Print second list
        print day

Edit: since you now told us your purpose below, I think all you need is this.

sudonym
  • 3,788
  • 4
  • 36
  • 61
0

Many thanks for your review and suggestion which works perfectly. I was thinking of something similar to your suggestion i.e creating two lists so I am glad to see that as a novice to python programming I am thinking the right way. Please see below your suggestion integrated into my original code to deliver what I was attempting;

 #This programme is intended to find the number of day of week for N-th day (mar) of a year provided that in this year January 1 is based on user entry (start_day). Days of week are numbered as: 0 — Sunday, 1 — Monday, 2 — Tuesday, ..., 6 — Saturday. An integer K in the range 1 to 365 is given.                        
start_day = int (input("Enter day number to start:- ")) 
mar = int(input("Enter nth day of the year to find:-"))    
count = 0      
days = ["0","1","2","3","4","5","6"]
days_before = []
while count < mar:
# Loop                
  for day in days: 

    if int(day) > start_day: 
      if count < mar:
        count += 1     # Either print
        print (day)    
    else:
        days_before.append(day)   # Or dump to second list
  else:                             # When finished
    for day in days_before: 
      if count < mar:
        count += 1       # Print second list
        print (day)  
  • I have up-voted but as I am new to the forum it doesn't seem to count. – CreditcruncherX Jun 08 '18 at 12:49
  • So all you want to do is to tell the weekday of, for example, the 136th day of year x, am I correct? Why not just use the datetime module? – sudonym Jun 08 '18 at 13:06
  • Correct. But it was an exercise I was given to complete whereby the days had to be represented by the numbers given in the list and return the 2n"th day using the allocated number. Believe it or not I passed the exercise with the code I cobbled together but I was aware it was cumbersome and kept thinking there must be a more elegant and flexible way to accomplish the goal. It wasn't a great exercise but it did get me thinking about iterating over all items in a list from a chosen index which meant i learnt something useful anyway. – CreditcruncherX Jun 08 '18 at 15:00
  • get it. Good attitude – sudonym Jun 08 '18 at 15:12
0
start = 4
for i in range(start, start+min(mar, len(days))):
    print(days[i % len(days)])

# mar = 4 --> 4, 5, 6, 0
# mar = 10 --> 4, 5, 6, 0, 1, 2, 3
BallpointBen
  • 9,406
  • 1
  • 32
  • 62