Python Forum
Accessing data in zip - Index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing data in zip - Index out of range
#11
Here is a version that works with the new specs
import itertools as it

def foo(names, k):
    n = len(names)
    for t in it.product(range(n), repeat=k):
        yield [names[x % n] for x in it.accumulate(t)]

names=["Jack","Jeni","Monsa","Mehus","Kuis","Tim","Tony","Yestgf","Pere"]
        
for x in foo(names, 4):
    print(' '.join(x))
Reply
#12
Great, it is working, however we got rid of enumerate and zip completely? :( cant we use it?
Reply
#13
There is nothing wrong in not using enumerate and zip.
Reply
#14
Thanks for moving this to homework, although it is not a homework . I am trying to learn python slowly and i hope i would be helped achieving it :) by moving this to homework i hope you would treat me as a student and clear my queries whenever time permits for you. :)
Reply
#15
This may look like a repeated question, but oh my gosh, its confusing for me, that is why i am posting in homework although it is not homework. i have 3 lists
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 want them to be manipulated to get the following things

i have a number and i want that number to be divided by length of order and the result will be the first range of n ranges and i want them to be further divided by the sum of years to get each share and then multiply that share according to the years, on top of all these i want the names to be printed using enumerate which i got as follows
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 idx,name in enumerate(Names):
    for idx in range(idx,idx+len(Names)):
        for idy in range(idx,idx+len(Names)):
            for idz in range(idy,idy+len(Names)):
                for ida in range(idz, idz + len(Names)):
                    print(name,Names[idx%len(Names)],Names[idy%len(Names)],Names[idz%len(Names)],Names[ida%len(Names)])
Reply
#16
list3 = list1 + list2 # combined list
# if lists are of same length you can combine into dictionary with:
mydict = dict(zip(list1, list2))
Reply
#17
Best would be to explain what your end goal is (i.e. provide example end result). In your code you don't use Years and Order lists and it does not reflect your explaination what you want to achieve. Most certainly your approach is non pythonic at best, plainly wrong in worst case
Reply
#18
(Mar-12-2018, 09:30 AM)buran Wrote: Best would be to explain what your end goal is (i.e. provide example end result). In your code you don't use Years and Order lists and it does not reflect your explaination what you want to achieve. Most certainly your approach is non pythonic at best, plainly wrong in worst case
yes you are right, my approach has been non pythonic and that is why i am struggling. .sorry for that. Since i am learning i am trying out few things . I want to achieve this

I have data which are related to each other Name,Years,Order. I have a number input (in a range) which should be divided by the length of order(9 in this case). The result of which should be further divided by sum of all years(490 in this case) to get the one share and then they should be multiplied by corresponding year value to get the value of their share.
Quote:Example:
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]
Input number : range( 0,500,500/9)
Divide 500 by 9 : 55.55
Divide 55.55 by 490 (sum of years list) : 0.1133 (this gives us one share)
Multiply 0.1133 with each item in year list i.e : 17*0.1133 for Jack ,20*0.1133 for jeni and so on
the output should display
Jack Jack 1.92 (which is nothing but the result we got from last point/ 17*0.1133)
Jack Jeni 2.266 (20*0.1133)
Jack Monsa 2.9458
Jack Mehus 4.532


and so on
the next set should start with
Jeni Jeni 57.81
Jeni Monsa 58.5
and so on, basically i am dividing the number and getting the single share and assigning them to the names corresponding to their years , if i add all of them at the end it should come to 500.

the 3rd level should further be divided by same total no of years and again assign them to the respective names.
I am kind of struck at manipulating these things :(
Reply
#19
What do these numbers represent, and why display them in the order Jack Jack, ... Jack Pere, Jeni Jeni, ... ?
Reply
#20
which number you are asking about?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,888 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,326 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  List index out of range when turning CSV into dict ranbarr 15 6,403 May-12-2021, 10:38 AM
Last Post: ranbarr
  list index out of range mcgrim 2 2,901 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 3,842 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,512 Aug-17-2018, 11:18 PM
Last Post: felie04
  "List index out of range" for output values pegn305 3 5,292 Nov-26-2017, 02:20 PM
Last Post: heiner55
  list index out of range DrPengin 1 3,682 Nov-09-2017, 08:35 PM
Last Post: gruntfutuk
  string index out of range cusick11 9 15,083 Mar-03-2017, 11:45 PM
Last Post: ichabod801
  Invalid syntax: string index out of range darkreaper1959 6 6,141 Jan-22-2017, 06:24 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020