简体   繁体   English

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

[英]Python- Calling elements from a list returns wrong ones

from this list, from which I shuffle: 从这个列表中,我从列表中拖曳:

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

I try to call it from one specific number at a time, 我尝试一次从一个特定的号码呼叫它,

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

but whenever I do I get this: 但是只要我这样做,我都会得到:

#an actual empty space
[
6
,

The code from this is part is meant to draw a figure and rotate it. 这部分的代码旨在绘制图形并旋转它。 We want to do so a different speeds, so that's why we created a list from which it takes one of those rotation speeds and uses it. 我们想要以不同的速度进行操作,因此这就是为什么我们创建一个列表的原因,该列表将采用其中一种转速并使用它。 The figure actually moves for each trial at a different speed, so the process of taking one value at a time works. 实际上,每个试验的图形以不同的速度移动,因此一次取一个值的过程起作用。 I just don't get why whenever I ask to show me each value of the list, it does this. 我只是不明白为什么每当我要求显示列表的每个值时都会这样做。

I hope this is sufficiently clear; 我希望这一点很清楚。 please don't hesitate asking me any question. 请不要犹豫,问我任何问题。

I'm having no problems. 我没问题 Are you sure you're not surrounding converting your list to a string somewhere down the line?? 您确定您不是将列表转换为字符串吗? It's treating RotationSpeedArray as a string, which is why I ask. 它把RotationSpeedArray当作一个字符串来对待,这就是我问的原因。

random.shuffle will throw a TypeError if you had put quotes around the list, so I'm assuming there is code you're not showing us somewhere past that which is converting it to a string? 如果您在列表中加上引号,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)

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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