Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在获取程序的输出,但不是按要求的方式?_Python_Python 2.7_Python 3.x_Ipython - Fatal编程技术网

Python 我正在获取程序的输出,但不是按要求的方式?

Python 我正在获取程序的输出,但不是按要求的方式?,python,python-2.7,python-3.x,ipython,Python,Python 2.7,Python 3.x,Ipython,编写一个接受字符串作为输入的Python函数。 函数必须返回字符串中出现的数字0-9之和,忽略所有其他字符。如果字符串中没有数字,则返回0 我的代码: user_string = raw_input("enter the string: ") new_user_string = list(user_string) addition_list = [] for s in new_user_string: if ( not s.isdigit()): combine_stri

编写一个接受字符串作为输入的Python函数。 函数必须返回字符串中出现的数字0-9之和,忽略所有其他字符。如果字符串中没有数字,则返回0

我的代码:

user_string = raw_input("enter the string: ")
new_user_string = list(user_string)
addition_list = []
for s in new_user_string:
    if ( not s.isdigit()):
        combine_string = "".join(new_user_string)
        print ( combine_string)
    else:
        if ( s.isdigit()):
            addition_list.append(s)
            test = "".join(addition_list)
            output = sum(map(int,test))
            print ( output )
输出应为:

Enter a string: aa11b33
8
我的输出:

enter the string: aa11b33
aa11b33
aa11b33
1
2
aa11b33
5
8

这看起来有点像家庭作业

getsum = lambda word: sum(int(n) for n in word if n.isdigit())

getsum('aa11b33')
Out[3]: 8

getsum('aa')
Out[4]: 0
一件一件地解释它是如何工作的:

  • 如果
    n
    仅由一个或多个数字组成,则函数
    n.isdigit()
    返回
    True
    ,否则返回false。()
  • word中n的语法
    将在iterable
    word
    中的每个项上循环。由于
    word
    是一个字符串,python将每个字符视为一个单独的项
  • 操作
    sum(int(n)for n in word…
    将每个字符强制转换为
    int
    ,并获取所有字符的总和,而后缀
    if n.isdigit()
    则过滤掉所有非数字字符。因此,最终结果将只取字符串
    word
    中所有单个数字字符的总和
  • 语法
    lambda x:some expression using x
    构造了一个匿名函数,该函数将某个值
    x
    作为其参数,并在冒号后返回表达式的值。为了给这个函数命名,我们可以把它放在赋值语句的右边。然后我们可以像调用普通函数一样调用它。通常最好使用普通的
    def getsum(x)
    类型的函数定义,但是
    lambda
    表达式有时非常有用,因为如果您有一种一次性函数,您只需要将其用作函数的参数。一般来说,在python中,最好能找到一种避免它们的方法,因为它们不太可读
  • 下面是一个完整的示例:

    def sumword(word):
        return sum( int(n) for n in word if n.isdigit() )
    
    word = raw_input("word please: ")
    print(sumword(word))
    

    Python关心缩进:

    user_string = raw_input("enter the string: ")
    new_user_string = list(user_string)
    addition_list = []
    for s in new_user_string:
        if ( not s.isdigit()):
            combine_string = "".join(new_user_string)
            #print ( combine_string)
        else:
            if ( s.isdigit()):
                addition_list.append(s)
                test = "".join(addition_list)
                output = sum(map(int,test))
    print ( output )   #<------ change here 
    
    user\u string=raw\u输入(“输入字符串:”)
    新建用户字符串=列表(用户字符串)
    添加列表=[]
    对于新用户字符串中的:
    如果(不是s.isdigit()):
    combine_string=“”.加入(新用户_string)
    #打印(合并字符串)
    其他:
    如果(s.isdigit()):
    添加列表。附加
    test=“”.加入(添加列表)
    输出=总和(映射(整数,测试))
    打印(输出)#它应该是

    user_string = raw_input("enter the string: ")
    new_user_string = list(user_string)
    addition_list = []
    for s in new_user_string:
        if ( not s.isdigit()):
            combine_string = "".join(new_user_string)
        else:
            if ( s.isdigit()):
                addition_list.append(s)
                test = "".join(addition_list)
    
    output = sum(map(int,addition_list))
    print output
    
    你得到了你想要的结果有两个原因

  • 在if语句中,当它遇到一个非数字时,您告诉它打印最初输入的字符串。当您查看输出时,这非常有意义-字符串在看到a时打印,字符串在看到第二个a时打印,字符串未打印,未打印(对于一个),然后字符串最后一次使用b打印
  • 当for循环在列表中递增时,您正在打印输出,这意味着它每次都打印总数。将输出变量和print语句移到for循环之外修复了该问题

  • 我正在尝试的是python编码挑战。因为这显然是家庭作业/编码挑战材料,所以发布一篇关于他做错了什么的详细解释,而不是只粘贴正确答案而不做解释,这难道没有意义吗?@machineyearning是的。不过,其他答案现在也有了,所以我将把我的答案留作解决问题的替代方法。@Ben,然而,我很好奇为什么你的答案有效,我不完全理解你函数中的代码行;你的比我的效率高很多,这就是我为什么要问的原因。@Ben Oops,我刚刚编辑了这篇文章,并做了解释,因为我认为你已经完成了。如果你认为你的更好,请随意删除我的。很好的解释。