我不确定为什么我得到一个空列表我测试了这个函数。有人可以帮我解决这个问题吗?
def find_images_with_keyword(key_dict, keywords_list):
'''(dict of {str: list of str}, list of str) -> list
Given a keyword_dictionary and a list of keywords,
return the list of filenames associated with the given keywords.
The list of filenames should include all filenames having one or more
of the specified keywords(ie, a filename may be associated with only one,
not all of the keywords in the list).
>>> find_images_with_keyword({'dog': ['1.png', '2.png'], 'animal': ['1.png', '3.png']}, ['dog', 'animal'])
['1.png', '2.png', '3.png']
'''
new_list = []
for keyword in key_dict.items():
for filename in keywords_list:
if keyword == filename:
new_list.append(filename)
return new_list
答案 0 :(得分:0)
试试这个
<table>
<table style="width: 30%; margin-bottom: 5px;font-size:12px;float: left;">
<tr>
<td style="width: 10%; vertical-align: top;">
<div class="accent-color2" style="width: 100px; padding: 0px 10px;"><b>Comfort_Features:</b></div>
</td>
</tr>
<td style="width: 30%; padding-right: 5px; vertical-align: top;">
<ul style="padding-left: 10px;">
<li>Rear Center Folding With Storage and Pass-Thru Armrests</li>
<li>Carpet Floor Mats</li>
<li>Carpet Floors</li>
<li>Front Floor Mats</li>
<li>Chrome Interior Accents</li>
<li>Simulated Alloy Shift Knob Trim</li>
<li>Urethane Steering Wheel Trim</li>
</ul>
</td>
</table>
<table style="width: 30%; margin-bottom: 5px;font-size:12px;float: left;display: inline-block;">
<tr>
<td style="width: 10%; vertical-align: top;">
<div class="accent-color2" style="width: 100px; padding: 0px 10px;"><b>Drivetrain:</b></div>
</td>
</tr>
<td style="width: 30%; padding-right: 5px; vertical-align: top;">
<ul style="padding-left: 10px;">
<li>4.11 Axle Ratio</li>
</ul>
</td>
</table>
<table style="width: 30%; margin-bottom: 5px;font-size:12px;float: left;display: inline-block;">
<tr>
<td style="width: 10%; vertical-align: top;">
<div class="accent-color2" style="width: 100px; padding: 0px 10px;"><b>Engine:</b></div>
</td>
</tr>
<td style="width: 30%; padding-right: 5px; vertical-align: top;">
<ul style="padding-left: 10px;">
<li>140 Amps Alternator</li>
<li>600 Cca Battery Rating</li>
<li>Battery Saver</li>
<li>Maintenance-Free Battery</li>
<li>50 State Emissions</li>
<li>Auxiliary Oil Cooler</li>
</ul>
</td>
</table>
<table style="width: 30%; margin-bottom: 5px;font-size:12px;float: left;display: inline-block;">
<tr>
<td style="width: 10%; vertical-align: top;">
<div class="accent-color2" style="width: 100px; padding: 0px 10px;"><b>In_Car_Entertainment:</b></div>
</td>
</tr>
<td style="width: 30%; padding-right: 5px; vertical-align: top;">
<ul style="padding-left: 10px;">
<li>Uconnect Infotainment</li>
</ul>
</td>
</table>
<table style="width: 30%; margin-bottom: 5px;font-size:12px;float: left;display: inline-block;">
<tr>
<td style="width: 10%; vertical-align: top;">
<div class="accent-color2" style="width: 100px; padding: 0px 10px;"><b>Instrumentation:</b></div>
</td>
</tr>
<td style="width: 30%; padding-right: 5px; vertical-align: top;">
<ul style="padding-left: 10px;">
<li>Clock</li>
<li>Digital Odometer</li>
<li>Driver Information System</li>
<li>External Temperature Display</li>
<li>Tachometer Gauge</li>
<li>Multi-Function Display</li>
<li>Trip Computer</li>
<li>2 Trip Odometer</li>
<li>Low Fuel Level Warnings and Reminders</li>
</ul>
</td>
</table>
<table style="width: 30%; margin-bottom: 5px;font-size:12px;float: up;display: inline-block;">
<tr>
<td style="width: 10%; vertical-align: top;">
<div class="accent-color2" style="width: 100px; padding: 0px 10px;"><b>Lights:</b></div>
</td>
</tr>
<td style="width: 30%; padding-right: 5px; vertical-align: top;">
<ul style="padding-left: 10px;">
<li>Daytime Running Lights</li>
<li>Auto Delay Off Headlights</li>
<li>Led Taillights</li>
</ul>
</td>
</table>
</table>
编辑:根据您的版本添加def find_images_with_keyword(key_dict, keywords_list):
'''(dict of {str: list of str}, list of str) -> list
Given a keyword_dictionary and a list of keywords,
return the list of filenames associated with the given keywords.
The list of filenames should include all filenames having one or more
of the specified keywords(ie, a filename may be associated with only one,
not all of the keywords in the list).
>>> find_images_with_keyword({'dog': ['1.png', '2.png'], 'animal': ['1.png', '3.png']}, ['dog', 'animal'])
['1.png', '2.png', '3.png']
'''
found_imgs = set()
for keyword, value in key_dict.items():
if keyword in keywords_list:
found_imgs = found_imgs.union(set(value))
return found_imgs
print find_images_with_keyword({'dog': ['1.png', '2.png'], 'animal': ['1.png', '3.png']}, ['dog', 'animal'])
版本。
list