具有多个条件的字典的反向模糊匹配
Inverse fuzzy match on dictionary with multiple criteria
我正在尝试在 python 字典上使用多个条件进行反向模糊选择,但我无法实现。
我有一本包含天气信息的字典:
{'time': '2020-01-21',
'summary': 'Rain and dangerously windy throughout the day.',
'icon': 'rain',
'sunriseTime': 1579590780,
'sunsetTime': 1579625640,
'moonPhase': 0.9,
'precipIntensity': 4.0669,
'precipIntensityMax': 6.6666,
'precipIntensityMaxTime': 1579640520,
'precipProbability': 1,
'precipType': 'rain',
'temperatureHigh': 13.39,
'temperatureHighTime': 1579608240,
'temperatureLow': 11.49,
'temperatureLowTime': 1579645500,
'apparentTemperatureHigh': 13.11,
'apparentTemperatureHighTime': 1579608240,
'apparentTemperatureLow': 11.76,
'apparentTemperatureLowTime': 1579645500,
'dewPoint': 9.58,
'humidity': 0.88,
'pressure': 1024.8,
'windSpeed': 14.23,
'windGust': 23.47,
'windGustTime': 1579628880,
'windBearing': 72,
'cloudCover': 0.99,
'uvIndex': 1,
'uvIndexTime': 1579608060,
'visibility': 6.288,
'ozone': 406.5,
'temperatureMin': 8.41,
'temperatureMinTime': 1579569780,
'temperatureMax': 13.82,
'temperatureMaxTime': 1579639560,
'apparentTemperatureMin': 3.78,
'apparentTemperatureMinTime': 1579569360,
'apparentTemperatureMax': 13.54,
'apparentTemperatureMaxTime': 1579639560}
我想删除所有在文本中带有 "Time"、"High" 或 "Low" 的键。
下面的代码给出了我想要删除的所有键:
{k:v for (k,v) in forecast_dict[0].items() for x in ["Time", "High", "Low"] if x in k}
输出:
{'sunriseTime': 1579590780,
'sunsetTime': 1579625640,
'precipIntensityMaxTime': 1579640520,
'temperatureHigh': 13.39,
'temperatureHighTime': 1579608240,
'temperatureLow': 11.49,
'temperatureLowTime': 1579645500,
'apparentTemperatureHigh': 13.11,
'apparentTemperatureHighTime': 1579608240,
'apparentTemperatureLow': 11.76,
'apparentTemperatureLowTime': 1579645500,
'windGustTime': 1579628880,
'uvIndexTime': 1579608060,
'temperatureMinTime': 1579569780,
'temperatureMaxTime': 1579639560,
'apparentTemperatureMinTime': 1579569360,
'apparentTemperatureMaxTime': 1579639560}
当我尝试使用以下内容进行反向选择时:
{k:v for (k,v) in forecast_dict[0].items() for x in ["Time", "High", "Low"] if x not in k}
我找回了包含所有条目的整本词典。我认为问题在于列表解包,但如果它适用于直接过滤,它不应该适用于反向吗?
这就是你想要的吗?使用 any()
和生成器:
>>> {k:v for k, v in forecast_dict[0].items() if not any(x in k for x in["Time", "High", "Low"])}
{'time': '2020-01-21',
'summary': 'Rain and dangerously windy throughout the day.',
'icon': 'rain',
'moonPhase': 0.9,
'precipIntensity': 4.0669,
'precipIntensityMax': 6.6666,
'precipProbability': 1,
'precipType': 'rain',
'dewPoint': 9.58,
'humidity': 0.88,
'pressure': 1024.8,
'windSpeed': 14.23,
'windGust': 23.47,
'windBearing': 72,
'cloudCover': 0.99,
'uvIndex': 1,
'visibility': 6.288,
'ozone': 406.5,
'temperatureMin': 8.41,
'temperatureMax': 13.82,
'apparentTemperatureMin': 3.78,
'apparentTemperatureMax': 13.54}
我建议更改条件以使用 any
:
{k:v for k,v in forecast_dict[0].items()
if not any(x in k for x in ["Time", "High", "Low"])}
我正在尝试在 python 字典上使用多个条件进行反向模糊选择,但我无法实现。
我有一本包含天气信息的字典:
{'time': '2020-01-21',
'summary': 'Rain and dangerously windy throughout the day.',
'icon': 'rain',
'sunriseTime': 1579590780,
'sunsetTime': 1579625640,
'moonPhase': 0.9,
'precipIntensity': 4.0669,
'precipIntensityMax': 6.6666,
'precipIntensityMaxTime': 1579640520,
'precipProbability': 1,
'precipType': 'rain',
'temperatureHigh': 13.39,
'temperatureHighTime': 1579608240,
'temperatureLow': 11.49,
'temperatureLowTime': 1579645500,
'apparentTemperatureHigh': 13.11,
'apparentTemperatureHighTime': 1579608240,
'apparentTemperatureLow': 11.76,
'apparentTemperatureLowTime': 1579645500,
'dewPoint': 9.58,
'humidity': 0.88,
'pressure': 1024.8,
'windSpeed': 14.23,
'windGust': 23.47,
'windGustTime': 1579628880,
'windBearing': 72,
'cloudCover': 0.99,
'uvIndex': 1,
'uvIndexTime': 1579608060,
'visibility': 6.288,
'ozone': 406.5,
'temperatureMin': 8.41,
'temperatureMinTime': 1579569780,
'temperatureMax': 13.82,
'temperatureMaxTime': 1579639560,
'apparentTemperatureMin': 3.78,
'apparentTemperatureMinTime': 1579569360,
'apparentTemperatureMax': 13.54,
'apparentTemperatureMaxTime': 1579639560}
我想删除所有在文本中带有 "Time"、"High" 或 "Low" 的键。
下面的代码给出了我想要删除的所有键:
{k:v for (k,v) in forecast_dict[0].items() for x in ["Time", "High", "Low"] if x in k}
输出:
{'sunriseTime': 1579590780,
'sunsetTime': 1579625640,
'precipIntensityMaxTime': 1579640520,
'temperatureHigh': 13.39,
'temperatureHighTime': 1579608240,
'temperatureLow': 11.49,
'temperatureLowTime': 1579645500,
'apparentTemperatureHigh': 13.11,
'apparentTemperatureHighTime': 1579608240,
'apparentTemperatureLow': 11.76,
'apparentTemperatureLowTime': 1579645500,
'windGustTime': 1579628880,
'uvIndexTime': 1579608060,
'temperatureMinTime': 1579569780,
'temperatureMaxTime': 1579639560,
'apparentTemperatureMinTime': 1579569360,
'apparentTemperatureMaxTime': 1579639560}
当我尝试使用以下内容进行反向选择时:
{k:v for (k,v) in forecast_dict[0].items() for x in ["Time", "High", "Low"] if x not in k}
我找回了包含所有条目的整本词典。我认为问题在于列表解包,但如果它适用于直接过滤,它不应该适用于反向吗?
这就是你想要的吗?使用 any()
和生成器:
>>> {k:v for k, v in forecast_dict[0].items() if not any(x in k for x in["Time", "High", "Low"])}
{'time': '2020-01-21',
'summary': 'Rain and dangerously windy throughout the day.',
'icon': 'rain',
'moonPhase': 0.9,
'precipIntensity': 4.0669,
'precipIntensityMax': 6.6666,
'precipProbability': 1,
'precipType': 'rain',
'dewPoint': 9.58,
'humidity': 0.88,
'pressure': 1024.8,
'windSpeed': 14.23,
'windGust': 23.47,
'windBearing': 72,
'cloudCover': 0.99,
'uvIndex': 1,
'visibility': 6.288,
'ozone': 406.5,
'temperatureMin': 8.41,
'temperatureMax': 13.82,
'apparentTemperatureMin': 3.78,
'apparentTemperatureMax': 13.54}
我建议更改条件以使用 any
:
{k:v for k,v in forecast_dict[0].items()
if not any(x in k for x in ["Time", "High", "Low"])}