【问题标题】:Issue with returning multiple pieces of data from a function从函数返回多条数据的问题
【发布时间】:2019-10-05 21:36:57
【问题描述】:

我正在尝试创建一个函数来检查代码的每一行,并在每行的井号处和之后切断所有内容(如果存在),否则它将保持不变。我正在尝试返回相同的文本,但每一行都在井号处被截断。

问题是,当我打印文本时,我看到了结果,但是当我返回它时,什么也没有发生。

code_1='''
hello this is a test to remove anything after
 and to get #this letter out 
 def hello_world():        # this is a comment
    print("Hello world!") # this is another comment
print("I # like # pound sign # .")
'''

def remove_octothorpe_and_after(code):
    code_in_lines = code.splitlines()
    for i in code_in_lines:
        if '#' in i:
            index=i.index('#')
            aftertext=i[index:]
            i = i.replace(aftertext,"")
        new_code=("".join(i))
        return new_code

【问题讨论】:

  • 你只能从一个函数返回一次,所以你可能不想在for循环中这样做。
  • @jonrsharpe 谢谢我将返回移出循环但现在我只看到一行。我做错了什么?

标签: python string loops replace split


【解决方案1】:

所以在更新new_code 之后,如果你打印,它会显示所有的行。但是如果你返回它是空的,你可能不想把那个返回语句放在前面提到的 for 循环中。

在我的方法中,我将所有更新的行存储在一个列表中,稍后将它们加入一个字符串,然后返回该字符串。

因为new_code在每次迭代中都是不同的值。

def remove_octothorpe_and_after(code):
    code_in_lines = code.splitlines()
    upCode=[]
    for i in code_in_lines:
        new_code=""
        if '#' in i:
            ind=i.index('#')
            aftertext=i[ind:]
            i = i.replace(aftertext,"")
        new_code=("".join(i))
        updatedCode.append(new_code+"\n")
    s = ''.join(updatedCode) 
    return s

print(remove_octothorpe_and_after(code))

【讨论】:

    【解决方案2】:

    Return 不是这样工作的。相反,请使用列表:

    def foo():
        bar = []
        for x: 
            bar.append(y)
        return bar
    
    print(foo())
    

    或者转换成生成器:

    def baz():
        for x:
            yield y
    
    foobar = baz()
    
    for result in foobar:
        print(result)
    

    【讨论】:

      【解决方案3】:

      您在第一次迭代后立即返回,您会看到一个空结果,因为第一行是一个空字符串,因为它以:

      ''' hello this is a test to remove anything after

      如果你这样开始,你只会看到第一行。

      '''hello this is a test to remove anything after

      如果您将 return 1 缩进向左移动,您将只能看到 return 中的最后一行,因为您将在每次迭代中覆盖 new_code=("".join(i))

      你可以做的是在函数中创建一个列表来存储临时结果,当循环完成时返回"".join(tmp)

      如果您只想返回# 之前的第一部分,您可以拆分并返回索引为 0 的第一项并附加换行符以保留单独的行。

      code_1='''
      hello this is a test to remove anything after
       and to get #this letter out 
       def hello_world():        # this is a comment
          print("Hello world!") # this is another comment
      print("I # like # pound sign # .")
      '''
      
      def remove_octothorpe_and_after(code):
          code_in_lines = code.splitlines()
          tmp = []
          for i in code_in_lines:
              tmp.append(i.split("#")[0] + "\n")
          return "".join(tmp)
      
      print(remove_octothorpe_and_after(code_1))
      

      结果

      hello this is a test to remove anything after
       and to get 
       def hello_world():        
          print("Hello world!") 
      print("I
      

      Python demo

      【讨论】:

        猜你喜欢
        • 2011-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        相关资源
        最近更新 更多