为了账号安全,请及时绑定邮箱和手机立即绑定

Python使用索引签入列表

Python使用索引签入列表

小怪兽爱吃肉 2021-12-26 10:44:32
我正在编写一个脚本,其目标是检查每个国家/地区 ( country) 并根据索引是否存在supply列表中的条目。目标是为每个国家/地区打印出哪些条目存在和不存在。国家:country=['usa', 'poland', 'australia', 'china']用于检查country朝向中的每个项目的索引supply。# indexwater_country=water_<country>_countrygas_state=gas_<country>_stateoil_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 existFor country 'poland' following entries does not exist:oil_poland_cityFor country 'australia' following entries does not exist:water_australia_countryoil_australia _city   For country 'china' following entries does not exist:water_china_countrygas_china_stateoil_china_city  
查看完整描述

2 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

这将起作用:


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:

    missing = [search_string.format(country) for search_string in search_strings if search_string.format(country) not in supply]

    if missing:

        missing_string = '\n'.join(missing)

        print(f"For country '{country}' the following entries do not exist:\n{missing_string}\n")


    else:

        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_state

oil_poland_city


For country 'australia' the following entries do not exist:

water_australia_country

oil_australia_city


For country 'china' the following entries do not exist:

water_china_country

gas_china_state

oil_china_city


查看完整回答
反对 回复 2021-12-26
?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

您可以简单地遍历每个国家/地区的所有供应品并检查满足哪些供应品,然后将未满足的供应品添加到某些列表中,此处的值为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:

    missing_supplies[c] = []

    for i in index:

        if i.replace('<country>', c) not in supply:

            missing_supplies[c].append(i.replace('<country>', c))


for c in country:

    if not missing_supplies[c]:

        print('For country \'{}\' all entries exist'.format(c))

    else:

        print('For country \'{}\' following entries does not exist:'.format(c))

        for v in missing_supplies[c]:

            print(v)

    print()

输出:


For country 'usa' all entries exist


For country 'poland' following entries does not exist:

gas_poland_state

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


查看完整回答
反对 回复 2021-12-26
  • 2 回答
  • 0 关注
  • 175 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信