2015-05-01 84 views
-1

我需要一些幫助與我的代碼,我存儲數組中的字符串列表,當我嘗試打印字符串列表,它不會讓我打印它們當我在循環中使用變量時。如何從數組中打印字符串列表

pos_start = list() 
pos_top = list() 
pos_width = list() 
pos_height = list() 
prog_title = list() 

for ind, row in enumerate(programs): 
    programs_top = 315 
    program_height = 33 
    program_gap = 3 
    position_start = start_pos 
    position_top = programs_top + channel_index * (program_height + program_gap + 1.5) 

    #create width size for per program button 
    if program_length >= 10 and program_length <= 45: #30 mins 
     program_width = 342 

    pos_start.append(position_start) 
    pos_top.append(position_top) 
    pos_width.append(program_width) 
    pos_height.append(program_height) 
position_start = map(str, pos_start) 
position_top = map(str, pos_top) 
program_width = map(str, pos_width) 
program_height = map(str, pos_height) 
program_title = map(str, prog_title) 

for position_start, position_width, position_height, program_title in zip(pos_start, pos_width, pos_height, prog_title): 
    print position_start 

這裏是position_start輸出:

13:41:23 T:4812 NOTICE: ['375', '1073', '1771', '2120', '2469', '2818', '3167', '3516', 
'3865', '4563', '5261', '5959', '6657', '7355', '7704', '8053', '8402', '8751', '9100', '9449', 
'9798', '10147', '10496', '10845', '11543', '12241', '12939', '13288', '13637', '13986', '14335', 
'15033', '15731', '16080', '16429', '16778', '17127', '17476', '17825', '18174', '18523', '18872', 
'19221', '19570', '19919', '20268', '20617', '20966', '21315', '21664', '22013', '22362', '22711', 
'23060', '23409', '23758', '24107', '24456', '24805', '25154', '25503', '25852', '26201', '26550', 
'26899', '27248', '27597', '28295', '28993'] 

我可以能夠使用print position_start外的for循環。

你能告訴我如何打印每個字符串,我想從數組中讀取字符串嗎?

我嘗試使用for position_start, position_width, position_height, program_title in zip(pos_start, pos_width, pos_height, prog_title):打印每個字符串,但它不會讓我。

任何想法?

+0

您的預期輸出是什麼? – ZdaR

回答

2
for item in position_start: 
    print item 

如果他們都是相同的長度(我不推薦並行陣列,因爲它很容易讓他們成爲一個項目關閉),你可以這樣做:

for i in range(0, len(position_start)): 
    print position_start[i] 
    print position_top[i] 
    print program_width[i] 
    print program_heigh[i] 
    print program_title[i] 

你也可以壓縮它們,就像您試圖獲取元組列表一樣。這將適用於甚至不均勻的列表。以下是一個示例:

import itertools 
l1 = [1, 2, 3] 
l2 = [4, 5, 6, 7] 
listZipped = itertools.izip_longest(l1, l2) 

for item in listZipped: 
    print item 
+0

其他變量'position_top','program_width', 'program_height'和'program_title'?我如何閱讀字符串列表? – Rob

+0

我的答案現在應該足夠了@Rob – heinst

+0

非常感謝你,我可以在position_start,position_top,program_width,program_height,program_title:中使用item1,item2,item3,item4,item5來打印字符串列表每個變量? – Rob