84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
代码如下:
def text2number(str): if '褒' in str: return 1 elif '贬' in str: return -1 else: return 0
闭关修行中......
text2number = lambda string: 1 if '褒' in string else -1 if '贬' in string else 0
In addition, it is not recommended that you use built-in method names as parameters. .
def text_to_id(text): mapping = { '褒': 1, '贬': -1, } return mapping.get(text, 0)
Shorter words:
def text_to_id(text): return { '褒': 1, '贬': -1, }.get(text, 0)
If it were shorter:
text_to_id = lambda text: { '褒': 1, '贬': -1, }.get(text, 0)
I also want to say that it is not recommended to use built-in class names str as parameter names.
str
You mean
1 if str.find('褒')!=-1 else -1 if str.find('贬')!=-1 else 0
Is this a trend
Available in actual testing, but not recommended
def text2number(str):
if not isinstance(str, unicode): str = str.decode('utf-8') if u'褒' in str: return 1 if u'贬' in str: return -1 return 0
In addition, it is not recommended that you use built-in method names as parameters. .
Shorter words:
If it were shorter:
I also want to say that it is not recommended to use built-in class names
str
as parameter names.You mean
Is this a trend
Available in actual testing, but not recommended
def text2number(str):