Python:raw_input问题,将我得到的值转换为数组

时间:2011-04-24 04:13:11

标签: python list raw-input

我再次遇到raw_input的问题,这次尝试将我获得的值转换为列表。这是我的代码:

original = raw_input("Type is your input? ")
original_as_array = list('original')
print original_as_array
for i in range(0,len(original)):
    print i

我的打印original_as_array字面打印['o', 'r', 'i'.... etc]。如果我们假装输入为Hello World,我希望original_as_array输出:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o'... etc]。我想我犯了一个小小的错误。你能指出我正确的方向吗:))

3 个答案:

答案 0 :(得分:3)

来自字符串文字的引号。

original_as_array = list(original)

答案 1 :(得分:1)

original = raw_input("Type is your input? ")
original_as_array = list(original) # no quotes. If you put quotes, you are turning it into a string.
print original_as_array
for i in original_as_array: # this is shorter way to iterate and print than your method
    print i

答案 2 :(得分:1)

字符串已经可以迭代,因此您无需将其转换为列表,因此您可以轻松地进行:

original = raw_input("Type is your input? ")
# or if you really want a list
original_as_list = list(original) # NOT 'original', that's passing the string original not the value of original
for letter in original:
    print letter