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

发布于 2024-11-03 05:25:30 字数 488 浏览 1 评论 0原文

我再次遇到 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

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

I'm having issues with raw_input again, this time trying to turn the value I get into a list. Here is my code:

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

my print original_as_array literally prints ['o', 'r', 'i'.... etc]. If we pretend that my input is Hello World, I want original_as_array to output: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o'... etc]. I think I'm making a tiny mistake. Can you point me in the right direction:)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-11-10 05:25:30

引号形成字符串文字。

original_as_array = list(original)

Quotes form a string literal.

original_as_array = list(original)
你在看孤独的风景 2024-11-10 05:25:30
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
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
悸初 2024-11-10 05:25:30

字符串已经是可迭代的,因此您不需要将其转换为列表,因此您可以轻松地执行:

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

Strings are already iterable, so you don't need to convert it to a list, so you can easily go :

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文