我不想统计一个项目的发生,而是多个项目的发生。变量match
包含字符串列表。我想搜索目标列表中的目标列表中有match
个项目。
match = ["Manager", "Access Responsible"]
我的词典包含以下条目:
dict = {'WF:ACAA-CR (auto)': ['Manager', 'Access Responsible', 'Automatic'],
'WF:ACAA-CR-AccResp (auto)': ['Manager', 'Access Responsible', 'Automatic'],
'WF:ACAA-CR-IT-AccResp[AUTO]': ['Group', 'Access Responsible', 'Automatic']}
此刻我的代码是:
for key, values in dict.items():
val = values
match = "Manager"
c = Counter(val)
print(key, c[match])
输出为:
WF:ACAA-CR (auto) 1
WF:ACAA-CR-AccResp (auto) 1
WF:ACAA-CR-IT-AccResp[AUTO] 0
那很好,我想要类似的东西,但是最终match
中有六个项目。
我尝试过:
for key, values in dict.items():
val = values
match = ["Manager", "Access Responsible"]
c = Counter(val)
print(key, c[match])
错误消息是:
回溯(最近通话最近): 在第29行的文件“ C:/Users/.PyCharmCE2018.2/config/scratches/extract.py” print(key,c [match]) TypeError:无法散列的类型:“列表”
如何计算列表中多个字符串的匹配项?
答案 0 :(得分:3)
您只需在 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<label>Name</label>
<input type="text" class="myText" id="name"> <!-- i only added name id to this element -->
<br>
<label>Age</label>
<input type="text" class="myText" id="age"> <!-- i only added age id to this element -->
<br>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong" onClick="submitText()"> <!-- here was an syntax error. you were calling method by uts name without () sign -->
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="bodyModal">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Confirm</button>
</div>
</div>
</div>
</div>
列表上循环并查找每个元素。另外,我看不到为什么需要额外的变量match
。
您的代码存在问题,因为您试图将完整列表val
作为参数传递给match
的字典输出。因此,您试图使用Counter
列表中的两个值作为键。因此,您得到了错误。
match
match = ["Manager", "Access Responsible"]
for key, values in dict_2.items():
val = values
c = Counter(val)
for m in match:
print(key, c[m])