Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Accessing data in zip - Index out of range
#1
i have 3 lists
Quote: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]

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):
Jack Jeni
File "C:/Users/Administrator/PycharmProjects/Tesrt/forumsquestion.py", line 7, in <module>
Jack Monsa
Jack Mehus
Jack Kuis
Jack Tim
Jack Tony
Jack Yestgf
Jack Pere
jdisplay = Names[j % len(Order)] if (j % len(Order)) else Names[j]
IndexError: list index out of range

Process finished with exit code 1

It looks like i am doing everything wrong Cry any help would be appreciated
Quote
#2
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))
Quote
#3
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))
Quote
#4
(Mar-03-2018, 06:57 PM)Larz60+ Wrote: 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))
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
Quote
#5
did you run what I gave you? Try it!
Quote
#6
(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 Sad part of that is as follows
Quote:Jack Jack
Jack Jeni
Jack Monsa
Jack Mehus
Jack Kuis
Jack Tim
Jack Tony
Jack Yestgf
Jack Pere
Jeni Jack
Jeni Jeni


See where the 2nd set starts :(
Quote
#7
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)
buran likes this post
Quote
#8
(Mar-04-2018, 06:41 AM)Gribouillis Wrote: 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)
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
Jack Jack Jack Jeni
Jack Jack Jack Monsa
Jack Jack Jack Mehus
Jack Jack Jack Kuis
Jack Jack Jack Tim
Jack Jack Jack Tony
Jack Jack Jack Yestgf
Jack Jack Jack Pere
Jack Jack Jeni Jack
Jack Jack Jeni Jeni
Quote
#9
if I get right your ever-changing goal, you just need to have similar construct with enumerate for every loop but the last one
Quote
#10
(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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  list index out of range mcgrim 2 248 May-25-2019, 07:44 PM
Last Post: mcgrim
  Use range to add char. to empty string johneven 4 964 Apr-15-2019, 11:21 PM
Last Post: scidam
  Accessing nested keys and values in Dictreader cartographer72 2 218 Mar-08-2019, 03:55 AM
Last Post: cartographer72
  switch case with range jun 3 373 Feb-27-2019, 05:35 AM
Last Post: scidam
  Need to use range with decimals KameronG 7 398 Feb-08-2019, 07:57 PM
Last Post: ichabod801
  IndexError: list index out of range abdullahali 4 457 Jan-17-2019, 07:54 AM
Last Post: buran
  Nested for loop issue always using index 0 searching1 2 363 Dec-30-2018, 09:17 AM
Last Post: searching1
  python homework print the sum of a range of numbers from x to y kirito85 3 532 Oct-28-2018, 08:56 AM
Last Post: kirito85
  Python Tutorial range function question HawkeyeKnight 2 487 Sep-27-2018, 09:45 AM
Last Post: ThiefOfTime
  String index out of range felie04 2 1,238 Aug-17-2018, 11:18 PM
Last Post: felie04

Forum Jump:


Users browsing this thread: 1 Guest(s)