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
#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


Messages In This Thread
RE: Multiplying number in a list in an order - by nilamo - Mar-22-2018, 06:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,624 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  Displaying list correspond to the column number danlopek14q 9 4,129 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  How to convert every even number in a list to odd? Bruizeh 4 3,892 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,662 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Get the biggest number from a two dimensional list rs74 13 4,241 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,393 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Appending to a list in the right order Noobstudent 2 2,394 Dec-07-2019, 10:39 PM
Last Post: Noobstudent
  adding a number to the list atux_null 4 3,998 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk
  Determine if a list contains a specific number of an item flannel_man 3 5,009 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