简体   繁体   English

我需要将用户输入变量作为此列表传递,例如:python 程序下方的 [1, 3, 6, 4, 1, 2]。 有人可以帮我吗

[英]I need to pass a user input variable as this list eg: [1, 3, 6, 4, 1, 2] in below python program. Could some one help me

def solution(A):
    sortedset= set(sorted(A))
    sol=1
    #for i in sortedset
    print(sortedset)
    for i in sortedset:
        if i == sol:
           sol+=1
        else:
            break
    print(sol)
A = input()
solution(A)

#A = [1, 3, 6, 4, 1, 2]

''While passing A = input() and input [1,3,6,4,1,2] not getting expected output '5' but if I give instead of user input directly like as A = [1, 3, 6, 4, 1, 2] i'm getting output '5'. ''虽然通过 A = input() 和输入 [1,3,6,4,1,2] 没有得到预期 output '5' 但如果我直接给出而不是用户输入,如 A = [1, 3, 6 , 4, 1, 2] 我得到 output '5'。 Please help me to fix this issue.请帮我解决这个问题。 '' ''

You can convert the string interpretation of a list that input returns to a list with ast.literal_eval :您可以使用ast.literal_evalinput返回的列表的字符串解释转换为列表:

import ast

A = ast.literal_eval(input())

As some people mentioned in the comments, the input function always returns a string.正如一些人在评论中提到的,输入 function 总是返回一个字符串。

However if you can always convert this string into your desired type.但是,如果您始终可以将此字符串转换为所需的类型。 For example:例如:

  1. Using a try block to handle the exception使用 try 块来处理异常
A = []
  try: 
    while True:
      A.append(int(input()))
  # if the input is not-integer, run the function
  except:
    solution(A)
  1. Using list comprehension:使用列表理解:
A = [int(item) for item in input("Enter the list items separated by space: ").split()]
solution(A)

This would work in python2 but not in 3 as the input is python 2's raw input and python 2's input has been removed这将在 python2 中起作用,但在 3 中不起作用,因为输入是 python 2 的原始输入,并且 python 2 的输入已被删除

you could use eval(input()) instead你可以使用 eval(input()) 代替

This is what you want I believe: i) string representation of list -> list ii) elements of list -> int我相信这就是你想要的:i)列表的字符串表示 -> 列表 ii)列表的元素 -> int

import ast
x = '[ "1", "2", "3" , "4", "5"]'
x = ast.literal_eval(x)
x = [int(val) for val in x ]
print(x)

[1, 2, 3, 4, 5] [1、2、3、4、5]

暂无
暂无

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

相关问题 我需要一些帮助在 python 中重新启动这个程序 - I need some help restarting this program in python 谁能帮我这个python singpath程序? - Could anyone help me with this python singpath program? Python-需要帮助,使用python将用户输入存储在列表中 - Python - Need Help Getting User input stored in a list using python 我对这个刽子手程序有点麻烦。 有人可以帮我弄清楚吗? - I am having a bit of trouble with this hangman program. Can someone help me figure it out? 有人可以帮我优化下面的 python 代码吗,我在某些测试用例中遇到超时异常 - Can someone help me in optimising the below python code, I am getting timeout exception for some of the test cases 您好,我的程序在 Python 中出现一些我不知道的错误,我已经在下面解释了所有内容 - Hello, I am getting some errors unknown to me in Python for my Program, I have explained everything below 为什么下面的代码返回一个空列表。我不明白它的原因,所以如果有人可以帮助我,那会让我高兴 - Why is the code below return an empty list.I could not understand the reason of it so if someone can help me about that , that will make me happy 我需要一些帮助来启动这个广播节目 - I need some help starting of this radio program 我需要用户输入来告诉程序使用哪个列表? - I need user input to tell the program which list to use? 创建列表的文本文件,然后在其他程序中重新访问该文本文件。 蟒蛇 - Creating a text file of list and then re-accessing the text file in some other program. Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM