Posts: 54
Threads: 9
Joined: Mar 2018
I have a list
Numbers=[4,7,8]
and i want to multiply that with a range(1,5,2) of numbers in an order
[inline]Expected output:
1 * 4
1 * 7
1 * 8
3 * 7 (2nd number should start from 2nd list number not from first)
3 * 8
3 * 4[/inline]
The basic code i wrote is as follows, how can i make sure that the 2nd number starts multiplying from 2nd number in list
1 2 3 4 |
Numbers = [ 4 , 7 , 8 ]
for i in range ( 1 , 5 , 2 ):
for j in Numbers:
print (i * j)
|
Posts: 54
Threads: 9
Joined: Mar 2018
Posts: 2,128
Threads: 11
Joined: May 2017
Mar-22-2018, 12:01 PM
(This post was last modified: Mar-22-2018, 12:02 PM by DeaD_EyE.)
You need something to roll the list:
1 2 3 |
def roll(data, n):
dlen = len (data)
return data[n % dlen:] + data[:n % dlen]
|
Or you just use numpy.roll(iterable, n) .
In your code you change the list Numbers after the inner loop finished.
1 2 3 4 5 6 7 |
numbers = [ 4 , 7 , 8 ]
for i in range ( 1 , 5 , 2 ):
for j in numbers:
print ( f '{i} * {j} = {i*j}' )
numbers = roll(numbers, 1 )
|
Posts: 54
Threads: 9
Joined: Mar 2018
:( it works, but i am extremely sad that i dont even know how to proceed with this, how can anyone think in pythonic way, any help?
Posts: 2,128
Threads: 11
Joined: May 2017
Mar-22-2018, 02:13 PM
(This post was last modified: Mar-22-2018, 02:14 PM by DeaD_EyE.)
It's important to understand the slicing syntax.
You can slice lists.
1 2 3 4 5 6 7 8 9 |
a_list = [ 0 , 1 , 2 , 3 , 4 , 5 ]
left_side = a_list[ 1 :]
right_side = a_list[: 1 ]
rolled_new_list = left_side + right_side
|
I still hope that someone is coming up with an itertools solution.
Posts: 8,167
Threads: 160
Joined: Sep 2016
Mar-22-2018, 04:41 PM
(This post was last modified: Mar-22-2018, 04:45 PM by buran.)
(Mar-22-2018, 08:57 AM)pythoneer Wrote: I have a list
Numbers=[4,7,8]
and i want to multiply that with a range(1,5,2) of numbers in an order
[inline]Expected output:
1 * 4
1 * 7
1 * 8
3 * 7 (2nd number should start from 2nd list number not from first)
3 * 8
3 * 4[/inline]
The basic code i wrote is as follows, how can i make sure that the 2nd number starts multiplying from 2nd number in list
1 2 3 4 |
Numbers = [ 4 , 7 , 8 ]
for i in range ( 1 , 5 , 2 ):
for j in Numbers:
print (i * j)
|
Dude, you have great problem not able to explain your goal in simple terms.
You say that you want to multiply numbers from one list by numbers from second one. can you explain where 3 comes in your example?! It's not in your second list and probably you are the only one who understands where it comes from...
Albert Einstein Wrote:"If you can't explain it to a six year old, you don't understand it yourself."
Posts: 54
Threads: 9
Joined: Mar 2018
3 comes from range values (1,5,2) 1 3
Posts: 8,167
Threads: 160
Joined: Sep 2016
Mar-22-2018, 06:41 PM
(This post was last modified: Mar-22-2018, 06:41 PM by buran.)
I didn't realize it's a range object.
1 2 3 4 5 6 7 8 |
from collections import deque
nums = deque([ 4 , 7 , 8 ])
for x in range ( 1 , 5 , 2 ):
for num in nums:
print ( '{}*{}={}' . format (x, num, x * num))
nums.rotate( - 1 )
|
Output: 1*4=4
1*7=7
1*8=8
3*7=21
3*8=24
3*4=12
It's not clear what should happen if range object has more elements than your list. e.g. for range(1, 10, 2) the output would be
Output: 1*4=4
1*7=7
1*8=8
3*7=21
3*8=24
3*4=12
5*8=40
5*4=20
5*7=35
7*4=28
7*7=49
7*8=56
9*7=63
9*8=72
9*4=36
Posts: 3,458
Threads: 101
Joined: Sep 2016
Ok, so you're starting with 4, 7, 8 , and you're multiplying each item by range(1, 5, 2) , which is 1, 3 .
That, right there, could be solved with itertools.product. But that's not the end of the story. You also want to rotate the base list each time you iterate over it. I would be surprised if itertools had something that could handle that, since it seems like a non-standard need.
But that's fine, because it isn't all that complicated anyhow :p
The key to solving this is indexing/slicing. The rest of it you can probably cobble together in any number of different ways, heck you can even skip the slicing ( list.pop(0) followed by .append() ).
1 2 3 4 5 6 7 8 9 |
>>> items = [ 4 , 7 , 8 ]
>>> for ndx in range ( 1 , 5 , 2 ):
... print ( list ( map ( lambda x: x * ndx, items)))
... first = items[ 0 ]
... rest = items[ 1 :]
... items = rest + [first]
...
[ 4 , 7 , 8 ]
[ 21 , 24 , 12 ]
|
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Mar-22-2018, 06:41 PM)buran Wrote:
1 |
from collections import deque
|
Fantastic. I can't believe I never knew about that.
|