简体   繁体   中英

how to unpack the list of elements

I need some help with my code, I'm storing multiple lists of elements, and when I try to print the lists, it only prints one element, where I have more than one.

When I try this:

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

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

for position_start, position_top, program_width, program_height in zip(position_start, position_top, program_width, program_height):

    pos_start = int(float(position_start))
    pos_top = int(float(position_top))
    pos_width = int(float(program_width))
    pos_height = int(float(program_height))
    print pos_start

Here is the list of element for pos_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']

Here is the result:

16:12:22 T:6852  NOTICE: 375

Can you please tell me how I can print each element from the list using the for loop that I have created?

The assignment on this line:

pos_start = int(float(position_start))

Overwrites your earlier variable which contains the list. You must use a different variable name in order to avoid the overwrite.

Also, calling int(float(position_start)) doesn't really make sense. Why are you coercing the type twice? This should be the same as simply int(position_start) .

Similarly, your line:

position_start, position_top, program_width, program_height in zip(position_start, position_top, program_width, program_height):

Has name collisions as well. This will overwrite the original variables.

Regarding your comment, there's not an easy way to create a new variable name for each loop iteration, but you can treat a list as a logical list of variables at each index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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