在行,
Boys dance, girls dance, he dances, she dances
如何计算dance
发生的次数而不计算dances
发生的次数(使用Python程序)?
方法是
>>> var = "Boys dance, girls dance, he dances, she dances"
>>> count1 = var.count("dance")
>>> count2 = var.count("dances")
>>> count = count1 - count2
>>> count
2
>>>
但它太长了。
是否有任何一行方法可用?
答案 0 :(得分:3)
这将简单地计算表达式" dance"出现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-animate.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>AngularJS</title>
</head>
<body>
<div ng-app="myApp">
<input type="text" ng-model="data.message">
<h1>{{data.message}}</h1>
<div ng-controller="FirstCtrl">
<input type="text" ng-model="data.message">
<h1>{{ data.message}}</h1>
</div>
<div ng-controller="SecondCtrl">
<input type="text" ng-model="data.message">
<h1>{{ data.message}}</h1>
</div>
</div>
<script type = "text/javascript" src="main.js"></script>
</body>
</html>
本网站应该为您提供有关正则表达式lib的更多信息。 https://docs.python.org/2/library/re.html
答案 1 :(得分:3)
尝试以下
echo "
<tr>
<td width=240 height=20 align='left' valign='top' style='cursor:hand;cursor:pointer;padding-top:".$pad. ";padding-bottom:5px' onclick=\"getLinks('".$mid. "');return false;\"><b class='news_date'>".$mdate." ".$cname.$mpol.":</b>
<font class='news_title'>".$msectname."</font><br>
<font class='news_date'>".$moretext."</font>
</td>
</tr>\n";
答案 2 :(得分:2)
一种选择是使用正则表达式:
ArrayList<Integer> myList = new ArrayList<Integer>();
for (int i = 0; i < myList.size(); i++) {
myList.remove(i);
}
答案 3 :(得分:1)
使用列表理解:在" "
拆分,从拆分中删除",."
v = "Boys dance, girls dance, he dances, she dances"
print(len([x for x in v.split() if x.strip(".,") == "dance"]))
使用列表推导的Finding occurrences of a word in a string in python 3中的答案并不涉及粘在分词上的问题 - 所以决定在这里回答。