Python使用索引签入列表

我正在编写一个脚本,其目标是检查每个国家/地区 ( country) 并根据索引是否存在supply列表中的条目。目标是为每个国家/地区打印出哪些条目存在和不存在。


国家:


country=['usa', 'poland', 'australia', 'china']

用于检查country朝向中的每个项目的索引supply。


# index

water_country=water_<country>_country

gas_state=gas_<country>_state

oil_city=oil_<country>_city   

供应:


supply=['water_usa_country', 'gas_usa_state', 'oil_usa_city', 'water_poland_country', 'gas_poland_city', 'gas_australia_state']  

对于上述情况,结果将出现在每个国家/地区的列表中:


For country 'usa' all entries exist


For country 'poland' following entries does not exist:

oil_poland_city


For country 'australia' following entries does not exist:

water_australia_country

oil_australia _city   


For country 'china' following entries does not exist:

water_china_country

gas_china_state

oil_china_city  


小怪兽爱吃肉
浏览 176回答 2
2回答

富国沪深

这将起作用:countries = ['usa', 'poland', 'australia', 'china']search_strings = ['water_{}_country', 'gas_{}_state', 'oil_{}_city']supply = ['water_usa_country', 'gas_usa_state', 'oil_usa_city', 'water_poland_country', 'gas_poland_city', 'gas_australia_state']for country in countries:&nbsp; &nbsp; missing = [search_string.format(country) for search_string in search_strings if search_string.format(country) not in supply]&nbsp; &nbsp; if missing:&nbsp; &nbsp; &nbsp; &nbsp; missing_string = '\n'.join(missing)&nbsp; &nbsp; &nbsp; &nbsp; print(f"For country '{country}' the following entries do not exist:\n{missing_string}\n")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(f"For country '{country}' all entries exist.\n")输出:For country 'usa' all entries exist.For country 'poland' the following entries do not exist:gas_poland_stateoil_poland_cityFor country 'australia' the following entries do not exist:water_australia_countryoil_australia_cityFor country 'china' the following entries do not exist:water_china_countrygas_china_stateoil_china_city

素胚勾勒不出你

您可以简单地遍历每个国家/地区的所有供应品并检查满足哪些供应品,然后将未满足的供应品添加到某些列表中,此处的值为missing_supplies.country = ['usa', 'poland', 'australia', 'china']index = ['water_<country>_country', 'gas_<country>_state', 'oil_<country>_city']supply = ['water_usa_country', 'gas_usa_state', 'oil_usa_city', 'water_poland_country', 'gas_poland_city', 'gas_australia_state']missing_supplies = {}for c in country:&nbsp; &nbsp; missing_supplies[c] = []&nbsp; &nbsp; for i in index:&nbsp; &nbsp; &nbsp; &nbsp; if i.replace('<country>', c) not in supply:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; missing_supplies[c].append(i.replace('<country>', c))for c in country:&nbsp; &nbsp; if not missing_supplies[c]:&nbsp; &nbsp; &nbsp; &nbsp; print('For country \'{}\' all entries exist'.format(c))&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('For country \'{}\' following entries does not exist:'.format(c))&nbsp; &nbsp; &nbsp; &nbsp; for v in missing_supplies[c]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(v)&nbsp; &nbsp; print()输出:For country 'usa' all entries existFor country 'poland' following entries does not exist:gas_poland_stateoil_poland_cityFor country 'australia' following entries does not exist:water_australia_countryoil_australia_cityFor country 'china' following entries does not exist:water_china_countrygas_china_stateoil_china_city
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python