在编译这个贪心算法的 python 问题时遇到问题
Having problem in compiling this Greedy Algorithm's python question
我正在尝试这道题,这是一道贪心算法题。庆典派对问题。
当我 运行 it 时,如下所示,它表示列表索引必须是整数。
你能帮我解决这个问题吗,我是算法编码的新手。
我也愿意接受更好更有效的解决方案。
问题:
a=[1,5.4,2.1,3.4,3.1,2.0,1.8,8.9,10,23,4,5,5,2,1.6,1.9]
a.sort()
q=0
z={}
for i in range(len(a)):
if (a[q]+1.0)>=a[i]:
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
else:
q=a[i]
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-60bee6e37157> in <module>
4 z={}
5 for i in range(len(a)):
----> 6 if (a[q]+1.0)>=a[i]:
7 if q not in z.keys():
8 z[q]=[]
TypeError: list indices must be integers or slices, not float
在 else
块的开头,您说 q=a[i]
。由于您在 a
中有浮点数,因此在循环中的某个时刻,q 被设置为浮点数。即使该浮点数类似于 2.0,当您尝试将其用作列表的索引时,python 仍会引发错误。要解决这个问题,您需要从列表 a
.
中删除所有浮点数
问题是在将 q 分配给 a
中的值后使用 q
a=[1,5.4,2.1,3.4,3.1,2.0,1.8,8.9,10,23,4,5,5,2,1.6,1.9]
a.sort()
q=0
z={}
for i in range(len(a)):
if (a[q]+1.0)>=a[i]: # this is the problem that you have an error
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
else:
q=a[i] #here you are assigned the value to q, which can be a float
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
当您检查 if (a[q]+1.0)>=a[i]
时,它会获取列表 a
并使用值 q
检查索引。由于该值可以是浮点数,因此您可能会出错,因为索引必须是整数。
您可以更改循环以改为跟踪索引:
a=[1,5.4,2.1,3.4,3.1,2.0,1.8,8.9,10,23,4,5,5,2,1.6,1.9]
a.sort()
q=0
qidx=0
z={}
for i in range(len(a)):
if (a[qidx]+1.0)>=a[i]:
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
else:
q=a[i]
qidx = i
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
哪个会输出
{0: [1, 1.6, 1.8, 1.9, 2.0, 2], 2.1: [2.1, 3.1], 3.4: [3.4, 4], 5: [5, 5, 5.4], 8.9: [8.9], 10: [10], 23: [23]}
我正在尝试这道题,这是一道贪心算法题。庆典派对问题。 当我 运行 it 时,如下所示,它表示列表索引必须是整数。 你能帮我解决这个问题吗,我是算法编码的新手。 我也愿意接受更好更有效的解决方案。
问题:
a=[1,5.4,2.1,3.4,3.1,2.0,1.8,8.9,10,23,4,5,5,2,1.6,1.9]
a.sort()
q=0
z={}
for i in range(len(a)):
if (a[q]+1.0)>=a[i]:
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
else:
q=a[i]
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-60bee6e37157> in <module>
4 z={}
5 for i in range(len(a)):
----> 6 if (a[q]+1.0)>=a[i]:
7 if q not in z.keys():
8 z[q]=[]
TypeError: list indices must be integers or slices, not float
在 else
块的开头,您说 q=a[i]
。由于您在 a
中有浮点数,因此在循环中的某个时刻,q 被设置为浮点数。即使该浮点数类似于 2.0,当您尝试将其用作列表的索引时,python 仍会引发错误。要解决这个问题,您需要从列表 a
.
问题是在将 q 分配给 a
中的值后使用 qa=[1,5.4,2.1,3.4,3.1,2.0,1.8,8.9,10,23,4,5,5,2,1.6,1.9]
a.sort()
q=0
z={}
for i in range(len(a)):
if (a[q]+1.0)>=a[i]: # this is the problem that you have an error
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
else:
q=a[i] #here you are assigned the value to q, which can be a float
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
当您检查 if (a[q]+1.0)>=a[i]
时,它会获取列表 a
并使用值 q
检查索引。由于该值可以是浮点数,因此您可能会出错,因为索引必须是整数。
您可以更改循环以改为跟踪索引:
a=[1,5.4,2.1,3.4,3.1,2.0,1.8,8.9,10,23,4,5,5,2,1.6,1.9]
a.sort()
q=0
qidx=0
z={}
for i in range(len(a)):
if (a[qidx]+1.0)>=a[i]:
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
else:
q=a[i]
qidx = i
if q not in z.keys():
z[q]=[]
z[q].append(a[i])
哪个会输出
{0: [1, 1.6, 1.8, 1.9, 2.0, 2], 2.1: [2.1, 3.1], 3.4: [3.4, 4], 5: [5, 5, 5.4], 8.9: [8.9], 10: [10], 23: [23]}