Python Forum
Multiplying number in a list in an order
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiplying number in a list in an order
#1
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
Numbers=[4,7,8]
for i in range(1,5,2):
    for j in Numbers:
        print(i*j)
Reply
#2
any help on this?
Reply
#3
You need something to roll the list:

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.

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)
    # here you have rolled the list 1 element to the right side
    # the new list is assigned to the name numbers
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
:( 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?
Reply
#5
It's important to understand the slicing syntax.
You can slice lists.

a_list = [0, 1, 2, 3, 4, 5]

left_side = a_list[1:] # [1, 2, 3, 4, 5]
right_side = a_list[:1] # [0]
# left side is from the index 1 to the end
# right side is from start to index 1

rolled_new_list = left_side + right_side # [1, 2, 3, 4, 5, 0]
# + operation on two lists will return a new chained list.
I still hope that someone is coming up with an itertools solution.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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
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."
Reply
#7
3 comes from range values (1,5,2) 1 3
Reply
#8
I didn't realize it's a range object.

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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
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()).
>>> 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]
Reply
#10
(Mar-22-2018, 06:41 PM)buran Wrote:
from collections import deque
Fantastic. I can't believe I never knew about that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,523 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Displaying list correspond to the column number danlopek14q 9 3,936 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,735 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,356 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Get the biggest number from a two dimensional list rs74 13 4,031 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,250 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Appending to a list in the right order Noobstudent 2 2,303 Dec-07-2019, 10:39 PM
Last Post: Noobstudent
  adding a number to the list atux_null 4 3,843 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk
  Determine if a list contains a specific number of an item flannel_man 3 4,882 Nov-12-2016, 04:46 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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