Posts: 4,787
Threads: 76
Joined: Jan 2018
Mar-04-2018, 08:26 AM
(This post was last modified: Mar-04-2018, 08:29 AM by Gribouillis.)
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))
Posts: 54
Threads: 9
Joined: Mar 2018
Mar-04-2018, 09:18 AM
(This post was last modified: Mar-04-2018, 10:07 AM by buran.)
Great, it is working, however we got rid of enumerate and zip completely? :( cant we use it?
Posts: 4,787
Threads: 76
Joined: Jan 2018
There is nothing wrong in not using enumerate and zip.
Posts: 54
Threads: 9
Joined: Mar 2018
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. :)
Posts: 54
Threads: 9
Joined: Mar 2018
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)])
Posts: 12,030
Threads: 485
Joined: Sep 2016
list3 = list1 + list2 # combined list
# if lists are of same length you can combine into dictionary with:
mydict = dict(zip(list1, list2))
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-12-2018, 09:30 AM
(This post was last modified: Mar-12-2018, 09:30 AM by buran.)
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
Posts: 54
Threads: 9
Joined: Mar 2018
(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 :(
Posts: 4,787
Threads: 76
Joined: Jan 2018
What do these numbers represent, and why display them in the order Jack Jack, ... Jack Pere, Jeni Jeni, ... ?
Posts: 54
Threads: 9
Joined: Mar 2018
which number you are asking about?
|