【问题标题】:How to convert numbers to a number list in python? [duplicate]如何在python中将数字转换为数字列表? [复制]
【发布时间】:2012-09-24 16:39:58
【问题描述】:

可能重复:
How can I get a list as input from the user in Python?

我目前有这个:

c = eval(input("Enter a group of numbers "))
#say someone types 123
print (c)
#prints out 123

我想要这个:

c = eval(input("Enter a group of numbers "))
#say they enter 123
print (c)
#prints out [1,2,3]

我希望 123 最终成为 [1,2,3]。我该怎么做?

【问题讨论】:

  • 不要使用eval。另外,如果您使用的是 python 2,请不要使用 input。两者都会导致任意代码执行漏洞。
  • 我知道每个人都喜欢简单的代表,但是对于高质量的问答,常见问题确实需要关闭,而不是每次都用十几个相同的答案来回答。

标签: python


【解决方案1】:

您通常不应在用户输入中使用eval。有人可以键入一个将评估为恶作剧的语句。

出于同样的原因,您应该避免使用input(),因为它等同于eval(raw_input()),也可能导致恶作剧——有意或无意。

但是,您可以使用 ast.literal_eval 安全地将用户输入的 Python 解释为 Python 数据结构:

>>> import ast
>>> ast.literal_eval(raw_input('Type Python input: '))
Type Python input: 1,2,3
(1, 2, 3)
>>> ast.literal_eval(raw_input('Type Python input: '))
Type Python input: [1,2,3]
[1, 2, 3]
>>> ast.literal_eval(raw_input('Type Python input: '))
Type Python input: 123
123
>>> ast.literal_eval(raw_input('type a number: '))
type a number: 0xab
171

(在每种情况下,>>> Type Python input: 之后的第一行是我在raw_input() 中输入的内容

如果你想分开数字,你可以这样做:

>>> [int(c) for c in raw_input() if c in '1234567890']
1234
[1, 2, 3, 4]
>>> [int(c) for c in raw_input() if c in '1234567890']
123a45
[1, 2, 3, 4, 5]

请注意,非数字已被过滤。

【讨论】:

    【解决方案2】:

    怎么样?

    c = [int(x) for x in input("Enter a group of numbers ")]
    #list comprehension over the input function
    

    输入 123 结果为 [1, 2, 3]

    好的,假设对于 python 2.x(输入返回一个 int 对象)

    c = [int(x) for x in str(input("Enter a group of numbers "))]
    #added an str() function for iterating
    

    【讨论】:

    • 这在 python 2.x 中不起作用
    • @AshwiniChaudhary:会的。我刚刚用 Python 2.6 对其进行了测试。
    • 很高兴它对你有用。
    • @Blender 不,它不会,input() 将在 python 2.x 中返回一个整数,并且 for 循环将引发错误:TypeError: 'int' object is not iterable
    • @AshwiniChaudhary:我的错,我测试了一些不同的东西。
    【解决方案3】:

    首先,如下:

    c = eval(input("Enter a group of numbers "))
    

    等同于:

    c = eval(eval(raw_input("Enter a group of numbers ")))
    

    所以你现在调用 eval 两次。有关input 的更多信息,请访问here

    这是你想要的一个可能的解决方案:

    c = raw_input("Enter a group of numbers "))
    c = [int(i) for i in c]
    print(c)
    

    您当然可以将上面的示例简化为两行(实际上甚至是一行)。

    【讨论】:

      【解决方案4】:

      您可以将其转换为字符串,然后将字符串中的每个字符转换为数字。

      myStr = str(myInt) out = [int(i) for i in myStr]

      【讨论】:

        【解决方案5】:
        >>> s = '123'
        >>> [int(c) for c in s]
        [1, 2, 3]
        

        【讨论】:

          【解决方案6】:

          您可以使用map()将数字转换为ints:

          >>> map(int, '123')
          [1, 2, 3]
          

          【讨论】:

            【解决方案7】:
            In [32]: c=raw_input()
            123
            
            In [33]: map(int,c)
            Out[33]: [1, 2, 3]
            

            如果输入类似于1 2 3,则使用split()

            In [37]: c=raw_input()
            1 2 3
            
            In [38]: map(int,c.split())
            Out[38]: [1, 2, 3]
            

            【讨论】:

              猜你喜欢
              • 2020-10-09
              • 1970-01-01
              • 1970-01-01
              • 2019-10-16
              • 2021-06-11
              • 2021-02-01
              • 2011-11-23
              • 2013-11-16
              • 1970-01-01
              相关资源
              最近更新 更多