简体   繁体   English

如何从数组中打印字符串列表

[英]how to print the list of strings from the arrays

I need some help with my code, I'm stored the list of strings in arrays and when I try to print the list of strings, it would not let me to print them when I use the variables in the loop. 我的代码需要一些帮助,我将字符串列表存储在数组中,当我尝试打印字符串列表时,当我在循环中使用变量时,它不会让我打印它们。

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

Here is the output for 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']

I can be able to use the print position_start outside the for loop. 我可以在for循环外使用print position_start

Can you please tell me how I can print each string that I want to read the strings from the arrays? 您能告诉我如何打印要从数组中读取字符串的每个字符串吗?

I try to use for position_start, position_width, position_height, program_title in zip(pos_start, pos_width, pos_height, prog_title): to print each string but it won't let me. 我尝试使用for position_start, position_width, position_height, program_title in zip(pos_start, pos_width, pos_height, prog_title):打印每个字符串,但不会让我。

Any idea? 任何想法?

for item in position_start:
    print item

If they are all the same length (and I don't recommend parallel arrays as it is very easy for them to be one item off) you could do something like: 如果它们的长度相同(并且我不建议使用并行数组,因为它们很容易成为一件商品),您可以执行以下操作:

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]

You could also zip them like you were trying to do to get a list of tuples. 您也可以像试图获取元组列表那样压缩它们。 This will work for even or uneven lists. 这适用于偶数或不均匀的列表。 Here is an example: 这是一个例子:

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

for item in listZipped:
    print item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM