1

im trying to write a script in python that gives me each item in array A and what it corresponds in array B

let's say array A are Customers and array B are rooms

A = 'Customer 1','Customer 2','Customer 3','Customer 4'
B = 'room 1','room 2','room 3','room 4'

for Customers,rooms in zip(A,B):
    print Customers +' is in '+ rooms

and the output is:

Customer 1 is in room 1
Customer 2 is in room 2
Customer 3 is in room 3
Customer 4 is in room 4

now what if we have only 3 rooms in array B the output well ignore Customer 4

Customer 1 is in room 1   
Customer 2 is in room 2
Customer 3 is in room 3

my quistion is how to make it write for me all rooms are reserved if A>b

Customer 1 is in room 1
Customer 2 is in room 2
Customer 3 is in room 3
customer 4 all rooms are reserved
GIZ
  • 4,409
  • 1
  • 24
  • 43
Aymen Derradji
  • 198
  • 2
  • 3
  • 12

2 Answers2

3

You can use itertools.izip_longest over zip to fill in the shorter list with None values and update your logic like below:

from itertools import izip_longest

for customer, room in izip_longest(A, B):
    if room:
        print customer + ' is in ' + room
    else:
        print customer + ' all rooms are reserved'
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
2

I'm not 100% I understood what you meant by

how to make it write for me all rooms are reserved if A>b

but my inference is you want some sort of default value if you A>B. For this you can use itertools.zip_longest

from itertools import zip_longest

A = 'Customer 1','Customer 2','Customer 3','Customer 4'
B = 'room 1','room 2','room 3','room 4', 'room 5', 'room 6'

for Customer, rooms in zip_longest(A, B, fillvalue='all rooms reserved'):
    print(Customer, rooms)

If however you need to simply display all rooms reserved when A>B, then you you would just have an if statement and print...but that's too easy.

notorious.no
  • 4,919
  • 3
  • 20
  • 34