Jupyter Notebook 通过两个函数传递/编辑变量

问题描述

我试图通过两个函数传递一个变量并全局修改它。我不允许在函数内使用“全局变量”我得到错误SyntaxError: name 'variable' is parameter and global。不确定这是否是 Jupyter 全局/局部范围问题,为了解决它,我使用 variable1 和 variable2 将所做的更改分配给新对象,但我不想诉诸于此。

import logging
import re
import nltk
from nltk.stem import WordNetLemmatizer
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',level=logging.INFO)

variable = 'programmingses!'

def cleanText(variable):
    variable = variable.lower()
    Yamtern = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
    variable = Yamtern.sub('',variable)
    emoji = re.compile("["
                           u"\U0001F600-\U0001FFFF"  # emoticons
                           u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                           u"\U0001F680-\U0001F6FF"  # transport & map symbols
                           u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                           u"\U00002702-\U000027B0"
                           u"\U000024C2-\U0001F251"
                           "]+",flags=re.UNICODE)
    variable = emoji.sub(r'',variable)
    variable = re.sub(r"[,.\"!@#$%^&*(){}?/;`~:<>+=-]","",variable)
    print("New variable is = " + variable)
    return(variable)

cleanText(variable)

这会打印: 新变量是 = 编程

'程序设计'

def lemonFunction(x):
    lemmatizer = WordNetLemmatizer()
    variable = lemmatizer.lemmatize(x)
    print(variable)
    return(variable)

lemonFunction(variable)

这会打印: 编程!

'程序设计!'

def preformTwofunctions(variable):
    cleanText(variable)
    lemonFunction(variable)
    print(variable)
    return(variable)

preformTwofunctions(variable)

此打印:

新变量是 = 编程

编程!

编程!

'程序设计!'

所以对于lemonFunction,变量没有被全局修改,我无法一个一个地执行清理函数来依次清理文本。柠檬函数从原始变量输入开始。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)