gpt4 book ai didi

python - 如何解压元素列表

转载 作者:太空宇宙 更新时间:2023-11-03 17:44:40 25 4
gpt4 key购买 nike

我的代码需要一些帮助,我正在存储多个元素列表,当我尝试打印列表时,它只打印一个元素,而我有多个元素。

当我尝试这个时:

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

这是 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']

结果如下:

16:12:22 T:6852  NOTICE: 375

您能告诉我如何使用我创建的 for 循环打印列表中的每个元素吗?

最佳答案

该行的分配:

pos_start = int(float(position_start))

覆盖之前包含列表的变量。您必须使用不同的变量名称以避免覆盖。

此外,调用 int(float(position_start)) 也没有任何意义。为什么要强制类型两次?这应该与简单的 int(position_start) 相同。

同样,你的行:

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

也存在名称冲突。这将覆盖原始变量。

关于您的评论,没有一种简单的方法可以为每次循环迭代创建新的变量名称,但您可以将列表视为每个索引处的变量的逻辑列表。

关于python - 如何解压元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29989735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com