0

I have three variables that hold data and I want to print it out into three columns. The data is currently held like this: ['VLAN0001', 'VLAN0005', 'VLAN0006', 'VLAN0007', 'VLAN0009', 'VLAN0010', 'VLAN0020', 'VLAN0022', 'VLAN0025', 'VLAN0026', 'VLAN0027', 'VLAN0029', 'VLAN0031', 'VLAN0032', 'VLAN0036', 'VLAN0037', 'VLAN0039', 'VLAN0040', 'VLAN0042', 'VLAN0043', 'VLAN0046', 'VLAN0047', 'VLAN0050', 'VLAN0051', 'VLAN0055', 'VLAN0056', 'VLAN0058', 'VLAN0059', 'VLAN0060', 'VLAN0064', 'VLAN0066', 'VLAN0068', 'VLAN0072', 'VLAN0074', 'VLAN0075', 'VLAN0077', 'VLAN0080', 'VLAN0090', 'VLAN0100', 'VLAN0101', 'VLAN0102', 'VLAN0103', 'VLAN0104', 'VLAN0105', 'VLAN0106', 'VLAN0107', 'VLAN0108', 'VLAN0109', 'VLAN0110', 'VLAN0111', 'VLAN0112', 'VLAN0116', 'VLAN0117', 'VLAN0118', 'VLAN0143', 'VLAN0170', 'VLAN0198', 'VLAN0201', 'VLAN0202', 'VLAN0203', 'VLAN0204', 'VLAN0205', 'VLAN0206', 'VLAN0207', 'VLAN0210', 'VLAN0299', 'VLAN0801', 'VLAN0802', 'VLAN0803', 'VLAN0899', 'VLAN0900', 'VLAN0901', 'VLAN0902', 'VLAN0903', 'VLAN0904', 'VLAN0999'] ['GigabitEthernet9/11\r', 'GigabitEthernet9/11\r', 'GigabitEthernet9/11\r', 'GigabitEthernet9/11\r', 'GigabitEthernet9/15\r', 'GigabitEthernet9/15\r', 'Port-channel1\r', 'GigabitEthernet9/19\r', 'Port-channel1\r', 'Port-channel1\r', 'GigabitEthernet12/1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel16\r', 'Port-channel1\r', 'Port-channel1\r', 'GigabitEthernet9/29\r', 'GigabitEthernet9/29\r', 'GigabitEthernet9/29\r', 'GigabitEthernet9/10\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel16\r', 'Port-channel1\r', 'GigabitEthernet9/23\r', 'GigabitEthernet9/23\r', 'GigabitEthernet12/1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel16\r', 'Port-channel14\r', 'Port-channel1\r', 'Port-channel16\r', 'Port-channel3\r', 'Port-channel14\r', 'Port-channel14\r', 'Port-channel1\r', 'Port-channel14\r', 'Port-channel1\r', 'GigabitEthernet9/17\r', 'GigabitEthernet9/2\r', 'GigabitEthernet9/3\r', 'Port-channel1\r', 'GigabitEthernet9/6\r', 'Port-channel1\r', 'GigabitEthernet9/8\r', 'GigabitEthernet9/10\r', 'GigabitEthernet9/13\r', 'GigabitEthernet9/29\r', 'GigabitEthernet9/15\r', 'GigabitEthernet9/16\r', 'GigabitEthernet9/27\r', 'GigabitEthernet9/17\r', 'GigabitEthernet9/29\r', 'Port-channel1\r', 'Port-channel16\r', 'Port-channel1\r', 'GigabitEthernet9/17\r', 'GigabitEthernet9/18\r', 'GigabitEthernet9/19\r', 'GigabitEthernet9/24\r', 'Port-channel1\r', 'GigabitEthernet9/23\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'GigabitEthernet9/29\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r', 'Port-channel1\r'] [' 1d04h', ' 1d04h', ' 1d04h', ' 1d04h', ' 1w0d ', ' 1w0d ', ' 5w5d ', ' 3w3d ', ' 12w6d', ' 4w3d ', ' 1w0d ', ' 30w3d', ' 12w6d', ' 17w3d', ' 30w3d', ' 30w3d', ' 3w6d ', ' 3w6d ', ' 2w6d ', ' 4d00h', ' 12w6d', ' 12w6d', ' 17w3d', ' 12w6d', ' 4d01h', ' 4d01h', ' 1w0d ', ' 30w3d', ' 2w0d ', ' 17w3d', ' 12w6d']

And I want it to show up like this: ('VLAN0001', 'GigabitEthernet9/11\r', ' 1d04h')

I was doing it with itertools.izip, but after about 20 entries it maxing out and not displaying all of the data. Any recommendations on how I can fix this?

undrwatr
  • 1
  • 1
  • Check this out: http://stackoverflow.com/questions/1277278/python-zip-like-function-that-pads-to-longest-length – user3885927 Feb 24 '15 at 21:36

1 Answers1

0

Try the builtin zip. If your lists are A, B, C, then:

rows = zip(A, B, C)

With that, you can print with a for loop:

for row in rows:
    print row  # ('VLAN0001', 'GigabitEthernet9/11\r', ' 1d04h')
    # or you can format somehow:
    "%s %s %s" % row  # Change the formatting codes as you please
    "{0:s} {1:s} {2:s}".format(*row)  # or use new-style formatting
Spice
  • 352
  • 1
  • 5
  • That puts the data in the columns, but doesn't print it out right. Also it doesn't print out all of the data right.[('VLAN0001', 'GigabitEthernet9/11\r', ' 1d04h'), ('VLAN0005', 'GigabitEthernet9/11\r', ' 1d05h'), ('VLAN0006', 'GigabitEthernet9/11\r', ' 1d05h'), ('VLAN0007', 'GigabitEthernet9/11\r', ' 1d04h'), ('VLAN0009', 'GigabitEthernet9/15\r', ' 1w0d '), ('VLAN0010', 'GigabitEthernet9/15\r', ' 1w0d '), ('VLAN0020', 'Port-channel1\r', ' 5w5d '), ('VLAN0022', 'GigabitEthernet9/19\r', ' 3w3d '), ('VLAN0025', 'Port-channel1\r', ' 12w6d'), ('VLAN0026', – undrwatr Feb 24 '15 at 21:48
  • It still isn't printing out all of the information. Is there a maximum to the amount of data that can be sitting in the variable rows in this case? – undrwatr Feb 24 '15 at 21:57
  • The problem is that `C` is shorter (31, rather than 76) than the other two lists, so zipping terminates after taking 31 elements from each list. I'm not sure exactly where you got the data, but you should make sure you haven't missed some. Either that, or pad `C` with junk data, like `padded = [C[i] if i < len(C) else "" for i in range(len(A))]`. – Spice Feb 24 '15 at 22:03
  • Thanks, I hadn't noticed that. Let me look into my data and see what is happening. – undrwatr Feb 24 '15 at 22:41