相关疑难解决方法(0)

如何在Python中将字符串解析为float或int?

在Python中,我如何解析数字字符串,如"545.2222"相应的浮点值,545.2222?或者将字符串解析为"31"整数,31

我只是想知道如何将一个浮点数 解析str为a float,并且(单独)将一个int 解析str为一个int.

python floating-point parsing integer type-conversion

2108
推荐指数
21
解决办法
371万
查看次数

如何将字符串拆分为列表?

我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?

def split_line(text):

    # split the text
    words = text.split()

    # for each word in the line:
    for word in words:

        # print the word
        print(words)
Run Code Online (Sandbox Code Playgroud)

python split list text-segmentation

545
推荐指数
8
解决办法
170万
查看次数

我如何在Python 3中使用raw_input

import sys
print(sys.platform)
print(2**100)
raw_input()
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 3.1并且无法raw_input"冻结"dos弹出窗口.我正在阅读的书是Python 2.5,我使用的是Python 3.1

我该怎么做才能解决这个问题?

python python-3.x

523
推荐指数
6
解决办法
83万
查看次数

询问用户输入,直到他们给出有效的响应

我正在编写一个必须接受用户输入的程序.

#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")
Run Code Online (Sandbox Code Playgroud)

如果用户输入合理数据,这将按预期工作.

C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!
Run Code Online (Sandbox Code Playgroud)

但如果他们犯了错误,那就崩溃了:

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
  File "canyouvote.py", line 1, in …
Run Code Online (Sandbox Code Playgroud)

python validation loops user-input python-3.x

523
推荐指数
10
解决办法
42万
查看次数

ValueError:基数为10的int()的无效文字:''

我正在创建一个读取文件的程序,如果文件的第一行不是空白,则会读取接下来的四行.在这些行上执行计算,然后读取下一行.如果该行不为空,则继续.但是,我收到此错误:

ValueError: invalid literal for int() with base 10: ''.

它正在读取第一行但不能将其转换为整数.

我该怎么做才能解决这个问题?

代码:

file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
    infile = open(file_to_read, 'r')
    while file_to_read != " ":
        file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
        file_to_write = file_to_write + ".csv"
        outfile = open(file_to_write, "w")
        readings = (infile.readline())
        print readings
        while readings != 0:
            global count
            readings = int(readings)
            minimum = (infile.readline())
            maximum = (infile.readline())
Run Code Online (Sandbox Code Playgroud)

python

252
推荐指数
7
解决办法
118万
查看次数

将函数应用于列表的每个元素

如何将函数应用于变量输入列表?例如,filter函数返回true值,但不返回函数的实际输出.

from string import upper
mylis=['this is test', 'another test']

filter(upper, mylis)
['this is test', 'another test']
Run Code Online (Sandbox Code Playgroud)

预期的产出是:

['THIS IS TEST', 'ANOTHER TEST']
Run Code Online (Sandbox Code Playgroud)

我知道upper是内置的.这只是一个例子.

python function list

43
推荐指数
3
解决办法
10万
查看次数

python中一个输入的两个值?

这是一个简单的问题,我不想在这里问它,但我似乎无法在其他任何地方找到答案:是否有可能在一行Python中从用户那里获得多个值?

例如,在CI中可以做这样的事情:scanf("%d %d", &var1, &var2).但是,我无法弄清楚Python的等价物是什么.我认为它只是类似的东西var1, var2 = input("Enter two numbers here: "),但这不起作用,我不抱怨,因为如果它确实没有多大意义.

有没有人知道一个优雅和简洁的好方法?

python variables input

32
推荐指数
4
解决办法
17万
查看次数

将字符串列表转换为整数列表

如何将空格分隔的整数输入转换为整数列表?

输入示例:

list1 = list(input("Enter the unfriendly numbers: "))
Run Code Online (Sandbox Code Playgroud)

转换示例:

['1', '2', '3', '4', '5']  to  [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

python integer input list

13
推荐指数
2
解决办法
6万
查看次数

如何在数组+ PYTHON中输入?

嗨,我是python的新手,想要在数组中输入.关于数组没有很好地描述python doc.另外我觉得我对python中的for循环有一些打嗝.

我在python中提供了我想要的C代码片段:

C代码:

int i;

printf("Enter how many elements you want: ");
scanf("%d", &n);

printf("Enter the numbers in the array: ");
for (i = 0; i < n; i++)
    scanf("%d", &arr[i]);
Run Code Online (Sandbox Code Playgroud)

python

12
推荐指数
3
解决办法
17万
查看次数

如何从python中的raw_input创建一个列表?

所以我raw_input作为一些列表的输入.

x= raw_input()
Run Code Online (Sandbox Code Playgroud)

我输入的地方1 2 3 4 如果我只输入数字,我将如何将其转换为整数列表?

python list raw-input python-2.7

12
推荐指数
2
解决办法
6万
查看次数