0
votes

I didn't know how best to phrase this question since googling and searching here always led me to stuff which was more involved, I am pretty sure this is basic stuff but for the life of me I can't find a nice way to do the following

given an integer series, say

for x in range(0,36):

I want to link these indices to different lists, the first one is easy it's 1/6th of the main list, so I can use

int(n/6)

to get 6 sets of 0,1,2,3,4,5 mapped to my series

but if I want a series which alternates 0,0,1,1, (repeat) what's the simplest way to do this (so not just an odd/even check with alternating 0s/1s, since the list I am mapping to has 2 values in it, but the original list of 36 items repeats these items, so each 2 rows should map to the same thing

I'm too embarassed to post the steaming pile of nonsense I've tried so far.

to clarify, the left number is my ranged index, the right number is what I want to get based on the index

  • 0,0 (will map to 1st item in a list of 2 items)
  • 1,0 (will also map to 1st item in a list of 2 items)
  • 2,1 (will map to 2nd item in a list of 2 items)
  • 3,1 (will also map to 2nd item in a list of 2 items)
  • 4,0 etc
  • 5,0
  • 6,1
  • 7,1
  • 8,0
  • 9,0
  • etc
1
Do you mean list(itertools.islice(itertools.cycle([0, 0, 1, 1]), 36))? - Mechanic Pig
yeah sorry let me make it simpler I want to convert a list 0,1,2,3,4,5,6,7,8,9,10 etc to 0,0,1,1,0,0,1,1,0,0,1,1 etc (what I meant in the original post is I can convert 0,1,2,3,4,5,6,7,8,9,10,11,12,13.... to 0,0,0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2 etc by using int(index/6) when looping. I could easily get 0,1,0,1,0,1,0,1 by using some integer/mod logic to check if index of loop is even or odd but what I want is the 0s and 1s to appear twice before repeating - DM71
Can you provide an example input and your expected output? - Mechanic Pig
Yes list(itertools.islice(itertools.cycle([0, 0, 1, 1]), 36))? is probably working but to me that seems like an "advanced" way, I guess I just felt stupid thinking that there must be some simple muppet expression like n%X etc that I just was drawing a blank on I basically want to loop through a list of 36 strings, but based on the same loop index I want to grab a string from another list inside the loop, the other list has only 2 values, hence I need to get some kind of 0,0,1,1,0,0,1,1 logic in the loop - DM71
so now it works with import itertools (which I didnt have before) map = list(itertools.islice(itertools.cycle([0, 0, 1, 1]), 36)) and my loop for x in range(0,36): text0 =36itemlist[x] text1 = 6itemlist[int(x/6)] text2 = 2itemlist[map[x]] - DM71

1 Answers

1
votes

Such logic seems simple enough:

>>> [i % 4 // 2 for i in range(36)]
[0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]