从函数返回多个数据的问题

时间:2019-05-19 06:23:35

标签: python string loops replace split

我正在尝试创建一个函数,该函数检查代码的每一行,并在每行的井号后面及之后将所有内容都砍掉(如果存在的话),否则它将保持不变。我试图返回相同的文本,但每一行都用磅符号切掉。

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

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

4 个答案:

答案 0 :(得分:0)

您有2个选择:

  1. 在函数内创建一个局部变量,并向其中添加每一行,然后返回该局部变量。
  2. 使用yield而不是return,您将获得一个生成器,并且可以对其进行迭代。

使用yield的示例:

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 foo():
    for line in code_1.splitlines():
        line = line[:line.find('#')] if '#' in line else line
        yield line

v = foo()  # you will get a generator

for line in v:
    print(line)

输出:

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

使用局部变量的示例:

def foo():
    clean_code = ''
    for line in code_1.splitlines():
        line = line[:line.find('#')] if '#' in line else line
        clean_code += line + '\n'
    return clean_code

v = foo()
print(v)

答案 1 :(得分:0)

因此,在更新new_code之后,如果打印,它将显示所有行。但是,如果您将其返回为空,则可能不希望像前面提到的那样将该return语句放入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 :(得分:0)

返回不是那样的。而是使用列表:

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 :(得分:0)

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

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

如果您这样启动,则只会看到第一行。

new_code=("".join(i))

如果将return 1的缩进向左移动,则只会看到return的最后一行,然后在每次迭代中都将覆盖"".join(tmp)

您可以做的是在函数中创建一个列表以存储临时结果,并在循环完成后返回#

如果您只想返回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)) 之前的第一部分,则可以拆分并返回索引为0的第一项,并添加换行符以保持单独的行。

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

结果

@FunctionalInterface
public interface MiddleInterface<F,T,V>{
    boolean isBetween(F from, T to, V middleValue);
}

MiddleInterface<Integer, Integer, Integer> middleInterface = 
(x,y,z) -> x>=y && y<=z; // true

Python demo