Python-从列表中调用元素会返回错误的元素

时间:2016-04-21 18:58:43

标签: python list

从这个列表中,我从中移动:

RotationSpeedArray = [4,6,8,10] #in Hz
random.shuffle(RotationSpeedArray)

我尝试一次从一个特定号码拨打电话,

print RotationSpeedArray[0]
print RotationSpeedArray[1]
print RotationSpeedArray[2]
print RotationSpeedArray[3]

但每当我这样做时,我得到了这个:

#an actual empty space
[
6
,

这部分的代码是绘制图形并旋转它。我们希望以不同的速度这样做,这就是为什么我们创建一个列表,从中获取其中一个旋转速度并使用它。该图实际上以不同的速度移动每个试验,因此一次取一个值的过程起作用。我只是不明白为什么每当我要求向我展示列表的每个值时,它都会这样做。

我希望这是足够明确的;请不要犹豫,问我任何问题。

1 个答案:

答案 0 :(得分:4)

我没有问题。你确定你没有把你的列表转换成一个字符串吗?它将RotationSpeedArray视为一个字符串,这就是我要问的原因。

如果你在列表中加上引号,那么

random.shuffle会抛出一个TypeError,所以我假设有些代码你没有向我们展示那些将它转换为字符串的代码吗?

import random
RotationSpeedArray = [4,6,8,10] #in Hz
random.shuffle(RotationSpeedArray)

# the problem is happening here


print(RotationSpeedArray[0])
print(RotationSpeedArray[1])
print(RotationSpeedArray[2])
print(RotationSpeedArray[3])

print(RotationSpeedArray)

# To demonstrate your particular issue, if we convert the list to a string
# we get the same problem you're having
StringArray = str(RotationSpeedArray)

print(StringArray[0])
print(StringArray[1])
print(StringArray[2])
print(StringArray[3])

print(StringArray)

输出:

10
6
4
8
[10, 6, 4, 8]
[
1
0
,
[10, 6, 4, 8]