I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easiest way to do so would be to use two for loops but I am not sure. could anyone please tell me how this is done.
-
Yes you right it's correct approach.Artem Barger– Artem Barger2009-06-23 14:00:11 +00:00Commented Jun 23, 2009 at 14:00
-
don't know whenever it's a homework or not, but sounds like, please write it down for me.Artem Barger– Artem Barger2009-06-23 14:01:23 +00:00Commented Jun 23, 2009 at 14:01
-
1-1: No sample code to comment on.S.Lott– S.Lott2009-06-23 14:05:33 +00:00Commented Jun 23, 2009 at 14:05
-
1@asadm: People ask homework questions like this one all the time. It helps to label homework so we can provide hints instead of answers. This question is not about Python as much as it as about the nature of nested loops -- a common topic in first-year computer courses.S.Lott– S.Lott2009-06-23 14:14:42 +00:00Commented Jun 23, 2009 at 14:14
-
5@asadm: Please provide code. That's how we know you're DOING the homework instead of asking us to do the homework. If it's not homework, please provide code so we know what you tried and where your question arose. In general, please provide code.S.Lott– S.Lott2009-06-23 14:15:41 +00:00Commented Jun 23, 2009 at 14:15
|
Show 1 more comment
4 Answers
Here is another way
a = [i*j for i in xrange(1,11) for j in xrange(i,11)]
note we need to start second iterator from 'i' instead of 1, so this is doubly efficient
edit: proof that it is same as simple solution
b = []
for i in range(1,11):
for j in range(1,11):
b.append(i*j)
print set(a) == set(b)
1 Comment
Anurag Uniyal
rough timing show for 10000 iteration a takes .8 sec while b takes 2.5 se so abt 3 times slower b is
Just for fun (and the itertools-addicted SO readers) using only one for-loop:
from itertools import product
for i,j in product(xrange(1,11), xrange(1,11)):
print i*j
EDIT: using xrange as suggested by Hank Gay
3 Comments
Hank Gay
Might as well use xrange if you're bringing itertools.
Jochen Ritzel
Fyi, product is unneeded (but meh, C loops instead of python), python has syntax for this: product(range(1,11), range(1,11)) <=> (i,j for i in range(1,11) for j in range(1,11)) for x in (i*j for i in range(1,11) for j in range(1,11)): print x,
wr.
but then again you have >1 for-loops ;-)
You may not need the nested for-loop Solution.
A Single Loop with List Comprehension (as shown below) would suffice:
r_list = list(range(2, 11))
output = []
for m in r_list:
tmp = [m*z for z in r_list]
output.append(tmp)
print(output)
Or Simpler:
output = []
for m in list(range(2, 11)):
tmp = [m*z for z in list(range(2, 11))]
output.append(tmp)
print(output)
Prints:
[
[4, 6, 8, 10, 12, 14, 16, 18, 20],
[6, 9, 12, 15, 18, 21, 24, 27, 30],
[8, 12, 16, 20, 24, 28, 32, 36, 40],
[10, 15, 20, 25, 30, 35, 40, 45, 50],
[12, 18, 24, 30, 36, 42, 48, 54, 60],
[14, 21, 28, 35, 42, 49, 56, 63, 70],
[16, 24, 32, 40, 48, 56, 64, 72, 80],
[18, 27, 36, 45, 54, 63, 72, 81, 90],
[20, 30, 40, 50, 60, 70, 80, 90, 100]
]