Accessing data in zip - Index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Accessing data in zip - Index out of range (/thread-8683.html) |
Accessing data in zip - Index out of range - pythoneer - Mar-03-2018 i have 3 lists Quote:Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"] i am trying to extract data from it as follows Jack Jack Jack Jeni Jack Monsa Jack Mehus Jack Kuis Jack Tim Jack Tony Jack Yestgf Jack Pere Jeni Jeni Jeni Monsa Jeni Mehus Jeni Kuis Jeni Tim Jeni Tony Jeni Yestgf Jeni Pere Jeni Jack and so on, for this i am writing a code as below Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"] Years=[17,20,26,40,67,88,96,99,37] Order=[1,2,3,4,5,6,7,8,9] for names,years,orders in zip(Names,Years,Order): for i in range(1,(len(Order)+1)): for j in range(i,i+len(Order)): jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j] print(names,Names[j])i am getting following output Quote:Traceback (most recent call last): It looks like i am doing everything wrong any help would be appreciated RE: Accessing data in zip - Index out of range - Larz60+ - Mar-03-2018 I don't understand why the order and years lists are there, Names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"] # Years=[17,20,26,40,67,88,96,99,37] # Order=[1,2,3,4,5,6,7,8,9] # for names,years,orders in zip(Names,Years,Order): # for i in range(1,(len(Order)+1)): # for j in range(i,i+len(Order)): # jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j] # print(names,Names[j]) for name in Names: for name1 in Names: print('{} {}'.format(name, name1)) RE: Accessing data in zip - Index out of range - buran - Mar-03-2018 or you can use itertools.product from itertools import product names=["Jack", "Jeni", "Monsa", "Mehus", "Kuis", "Tim", "Tony", "Yestgf", "Pere"] for two_names in product(names, names): print(' '.join(two_names)) RE: Accessing data in zip - Index out of range - pythoneer - Mar-04-2018 (Mar-03-2018, 06:57 PM)Larz60+ Wrote: I don't understand why the order and years lists are there,i actually want this to print in an order, it starts from Jack Jack and then 2nd set starts from Jeni Jeni, that is why i have used the order list RE: Accessing data in zip - Index out of range - Larz60+ - Mar-04-2018 did you run what I gave you? Try it! RE: Accessing data in zip - Index out of range - pythoneer - Mar-04-2018 (Mar-04-2018, 01:05 AM)Larz60+ Wrote: did you run what I gave you? Try it!Yes, it didnt give me the output i was looking for part of that is as follows Quote:Jack Jack See where the 2nd set starts :( RE: Accessing data in zip - Index out of range - Gribouillis - Mar-04-2018 Simpler is better: names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"] for i, a in enumerate(names): for b in names[i:] + names[:i]: print(a, b) RE: Accessing data in zip - Index out of range - pythoneer - Mar-04-2018 (Mar-04-2018, 06:41 AM)Gribouillis Wrote: Simpler is better:it starts to malfunction if we go a bit deeper names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"] for i, a in enumerate(names): for b in names[i:] + names[:i]: for c in names[i:] + names[:i]: for d in names[i:] + names[:i]: print(a, b, c, d) Quote:Jack Jack Jack Jack RE: Accessing data in zip - Index out of range - buran - Mar-04-2018 if I get right your ever-changing goal, you just need to have similar construct with enumerate for every loop but the last one RE: Accessing data in zip - Index out of range - pythoneer - Mar-04-2018 (Mar-04-2018, 07:34 AM)buran Wrote: if I get right your ever-changing goal, you just need to have similar construct with enumerate for every loop but the last one :( sorry, i dont get you ,can you please show me the code |