Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python错误:TypeError:can';t将序列乘以类型为'的非整数;浮动';_Python_Python 2.7 - Fatal编程技术网

python错误:TypeError:can';t将序列乘以类型为'的非整数;浮动';

python错误:TypeError:can';t将序列乘以类型为'的非整数;浮动';,python,python-2.7,Python,Python 2.7,我的代码有问题。我试图在循环下使用变量channels\u index一次从列表中获取一个元素,而不保持循环,但我遇到了一个错误:TypeError:不能将序列乘以“float”类型的非int 当我尝试这个: dbconnect = con.cursor() dbconnect.execute("SELECT COUNT(*) FROM programs") x = dbconnect.fetchone()[0] for channels_range, total_channels in en

我的代码有问题。我试图在循环下使用变量
channels\u index
一次从列表中获取一个元素,而不保持循环,但我遇到了一个错误:
TypeError:不能将序列乘以“float”类型的非int

当我尝试这个:

dbconnect = con.cursor()
dbconnect.execute("SELECT COUNT(*) FROM programs")
x = dbconnect.fetchone()[0]

for channels_range, total_channels in enumerate(xrange(1, x + 1, 69), 1):
    pass
channels_index = range(0, channels_range)   # count how many channels I have got

for ind, row in enumerate(programs):
    program = row[1].encode('ascii'), str(row[2]), str(row[3])
    title = row[1].encode('ascii')
    program_start_date = str(row[2])
    program_end_date = str(row[3])

    program_height = 33
    program_gap = 3
    position_top = programs_top + channels_index * (program_height + program_gap + 1.5)
错误在这一行突出显示:

position_top = programs_top + channels_index * (program_height + program_gap + 1.5)
以下是元素列表:

18:22:32 T:3680  NOTICE: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
我连接到一个数据库,从每69行中获取数据,以计算在一个范围内有多少行。我不能将
17
的值放在该代码
的行中,因为我不知道数据库中有多少69行


你能帮助我如何在循环下使用变量
channels\u index
一次从列表中获取每个元素而不保持循环吗?

我不确定我是否完全理解你的要求,但是如果你试图从
channels\u index
中获取索引,一次一个,为什么不直接将它添加到
for
循环中呢

# same as above...
for (ind, row), ch_idx in zip(enumerate(programs), channels_index):
   # continue on as before
   position_top = programs_top + ch_idx * (program_height + program_gap + 1.5)
如果要在值0到
通道\u范围之间循环,请使用模数:

channels_index = 0

for ...:
    channels_index = (channels_index + 1) % channels_range
这将把
channels\u index
增加到
1
,然后增加到
2
,一直增加到
channels\u range-1
,然后返回到0

这里不需要预先生成一个值列表

如果您想计算69乘以0,然后再计算69乘以1,以此类推,直到16,您仍然可以简单地从行索引计算:

for ind, row in enumerate(programs):
    channel_index = ind // 69

行索引0到68除以69生成
0
。索引69到137给出了
1
,以此类推。

channels\u index
是一个列表,但
(程序高度+程序间隙+1.5)
产生一个浮动。你期望那里会发生什么?事实上,我并没有遵循你想要实现的目标。如果你试图循环遍历每69行
程序
,有更好的方法,比如@MartijnPieters Yes
channels\u index
是一个列表,但我希望他们一次从列表中获取一个值,示例:获取值
0
以输出它们,然后获取下一个值
1
以输出它们,依此类推。您可能想循环这些值?所以从
0
循环到
16
,然后返回到
0
?@MartijnPieters不,我想循环那些从
0
16
的值,而不是返回到
0
。非常感谢,但是不,我想循环使用从
0
16
的值,而不返回到
0
。请您更改代码。非常感谢,但在我的代码中,它将同时从0开始循环到16。在转到下一个值
1
之前,我想将值
0
循环69次,以此类推。我怎样才能做到这一点?我想把这些值循环69次,像这样:
0
0
0
0
0
0
0
0
0
,直到69次,然后循环下一个值,使其循环69次,
1
1
1
1
1
1
等@Rob:itertools呢?重复一遍?@MartijnPieters我在等你的答案。