Python Forum
print two different sequence number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print two different sequence number
#1
Hi all I'm studying python recently and I'm newbie on programming language also
I'm writing a pyhthon code that print two different sequence number, the first one composed with 1,2,3,4,..n the second one with 1,5,9.13,.m
So I'd like to obtain an output like this:

1 1
2 5
3 9
4 13
5 17
....

I've tried with the following "for loop nested" but the result is not what I'm looking for

numeri1 = range(1,10)
numeri2 = range(1,10,4)

for elemento_1 in numeri1:
   for elemento_2 in numeri2:
      if elemento_1 <= elemento_2:
      print (elemento_1, elemento_2)
Infact the output obtained using this code is the following
1 1
1 5
1 9
2 5
2 9
3 5
3 9
4 5
4 9
5 5
5 9
6 9
7 9
8 9
9 9

So..Have you got any suggestion for modify this code to get the correct output?
Anyway is the "for loop nested" the right way or there is another way?
Thanks
Riccardo
Reply
#2
numeri1 = range(1,10)
numeri2 = range(1,10,4)
 
for elemento_1, elemento_2 in zip(numeri1, numeri2):
    print (elemento_1, elemento_2)
Quote:1 1
2 5
3 9

https://docs.python.org/3/library/functions.html#zip

If the sequences haven't the same length, the shortest win.
  • numeri1 has 9 elements
  • numeri2 has 3 elements
So, it will not iterate over the remaining 6 elements from numeri1.

The function itertools.zip_longest will use by default None as fillvalue.
https://docs.python.org/3/library/iterto...ip_longest
from itertools import zip_longest


numeri1 = range(1,10)
numeri2 = range(1,10,4)
 
for elemento_1, elemento_2 in zip_longest(numeri1, numeri2):
    print (elemento_1, elemento_2)
Quote:1 1
2 5
3 9
4 None
5 None
6 None
7 None
8 None
9 None

I think it's not what you want. Should 1 5 9 repeat the whole time?
If it's the case:

from itertools import cycle


numeri1 = range(1,10)
numeri2 = cycle(range(1,10,4))
 
for elemento_1, elemento_2 in zip(numeri1, numeri2):
    print (elemento_1, elemento_2)
Now numeri1 has still 9 elements (1..9) and numeri2 if infinite.
If you've an infinite generator/iterator, then better not use zip_longest (it will never finish).
Just use zip and the length of numeri1 defines the complete length.
The itertools.cycle function takes an Iterable (e.g. list or tuple) and cycle through them, until the end has been reached and then it starts from beginning.

Or if you want to count until the first list is done:
https://docs.python.org/3/library/iterto...ools.count
from itertools import count


numeri1 = range(1,10)
numeri2 = count(1, 2) # <- this is infinite, so I don't have to think about length

for elemento_1, elemento_2 in zip(numeri1, numeri2):
    print (elemento_1, elemento_2)
Quote:1 1
2 3
3 5
4 7
5 9
6 11
7 13
8 15
9 17

Itertools is powerful.
In addition, you can see, that it's straightforward to exchange parts of the program without changing much of the rest.
You need the zip function at many places.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Hi Dead
thanks for your reply, I'll try your suggestions..
Best Regards
R.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print Player Name and Number of Pokemon webmanoffesto 1 1,067 Aug-23-2022, 08:54 PM
Last Post: deanhystad
  Sequence Generator - Long number krux_rap 3 2,279 Aug-19-2020, 06:37 PM
Last Post: buran
  Print the number of items in a list on ubuntu terminal buttercup 2 1,897 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Print 'X' a number of times Than999 1 2,611 Jan-18-2020, 06:41 PM
Last Post: Larz60+
  how do I make a number only print once but the number keeps on decreasing? syafiq14 5 2,889 Jan-03-2020, 10:34 AM
Last Post: perfringo
  print number of a list line per line lateublegende 2 2,685 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  Repeating same number in sequence Rudinirudini 6 4,166 Oct-28-2018, 07:44 PM
Last Post: DeaD_EyE
  How to get number with print statement Rehan11 1 2,253 Jul-28-2018, 02:01 PM
Last Post: buran
  Unable to print the exact Float values when I convert LIST to Sequence of Tuples? preethamalluri 1 2,384 Jul-12-2018, 09:03 AM
Last Post: buran

Forum Jump:

User Panel Messages

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