如何在python2.7中的函数中发送变量
我正在用Python2.7编写代码,需要弄清楚如何使“>”成为一个变量,在我调用指令时发送该变量。我需要多次调用此函数,有时需要“ 这是行不通的,因为您将“>”作为一个简单字符串传递,而Python不知道您打算将其作为运算符传递 如果您的问题是二进制(“”),一个非常简单的解决方案是,不传递字符串,而是传递布尔值,以确定使用哪个运算符:如何在python2.7中的函数中发送变量,python,function,variables,Python,Function,Variables,我正在用Python2.7编写代码,需要弄清楚如何使“>”成为一个变量,在我调用指令时发送该变量。我需要多次调用此函数,有时需要“ 这是行不通的,因为您将“>”作为一个简单字符串传递,而Python不知道您打算将其作为运算符传递 如果您的问题是二进制(“”),一个非常简单的解决方案是,不传递字符串,而是传递布尔值,以确定使用哪个运算符: sentiment_point = giving_points(index_focus_word, True, 1, sentence, -2) def gi
sentiment_point = giving_points(index_focus_word, True, 1, sentence, -2)
def giving_points(index_focus_word, greater, index_sentiment_word, sentence, location):
if greater:
if index_focus_word > index_sentiment_word:
sentiment_word = sentence[index_focus_word + location]
else: #....
else:
if index_focus_word < index_sentiment_word:
touction\u point=给出分数(索引焦点词,真,1,句子,-2)
def评分(索引焦点词、较大值、索引情感词、句子、位置):
如果更大:
如果索引焦点词>索引情感词:
情感词=句子[索引词+位置]
其他:#。。。。
其他:
如果索引关注词<索引情感词:
这是行不通的,因为您将“>”作为一个简单字符串传递,而Python不知道您打算将其作为运算符传递
如果您的问题是二进制(“”),一个非常简单的解决方案是,不传递字符串,而是传递布尔值,以确定使用哪个运算符:
sentiment_point = giving_points(index_focus_word, True, 1, sentence, -2)
def giving_points(index_focus_word, greater, index_sentiment_word, sentence, location):
if greater:
if index_focus_word > index_sentiment_word:
sentiment_word = sentence[index_focus_word + location]
else: #....
else:
if index_focus_word < index_sentiment_word:
touction\u point=给出分数(索引焦点词,真,1,句子,-2)
def评分(索引焦点词、较大值、索引情感词、句子、位置):
如果更大:
如果索引焦点词>索引情感词:
情感词=句子[索引词+位置]
其他:#。。。。
其他:
如果索引关注词<索引情感词:
操作符模块提供了实现Python操作符的函数。在这种情况下,您需要操作符.gt
import operator
sentiment_point = giving_points(index_focus_word, operator.gt, 1, sentence, -2)
def giving_points(index_focus_word, cmp, index_sentiment_word, sentence, location):
if cmp(index_focus_word, index_sentiment_word):
sentiment_word = sentence[index_focus_word + location]
operator
模块提供了实现Python操作符的函数。在本例中,您需要operator.gt
import operator
sentiment_point = giving_points(index_focus_word, operator.gt, 1, sentence, -2)
def giving_points(index_focus_word, cmp, index_sentiment_word, sentence, location):
if cmp(index_focus_word, index_sentiment_word):
sentiment_word = sentence[index_focus_word + location]
请通过高亮显示代码并按内嵌编辑器顶部的
{}
设置代码格式。请通过高亮显示代码并按内嵌编辑器顶部的{}
设置代码格式。
import operator
sentiment_point = giving_points(index_focus_word, operator.gt, 1, sentence, -2)
def giving_points(index_focus_word, cmp, index_sentiment_word, sentence, location):
if cmp(index_focus_word, index_sentiment_word):
sentiment_word = sentence[index_focus_word + location]