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
#11
yeah, it's very neat and is optimized for such operations
from docs:
Quote:Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short for “double-ended queue”). Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
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
#12
(Mar-22-2018, 06:41 PM)buran Wrote: 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

can we use list comprehensions with this?
Reply
#13
Use list comprehension where and why? At least you need outer loop in order to call deque.rotate()
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


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