Python 根据多种约束条件选择项目
根据一些限制条件,我访问了每个地点。例如,我每天在Python 根据多种约束条件选择项目,python,dictionary,if-statement,constraints,Python,Dictionary,If Statement,Constraints,根据一些限制条件,我访问了每个地点。例如,我每天在上午和中午去学校,而在周末不去学校。 另一个,我在早上时间去了购物中心,所以我可能今天和未来几天都不会再去那里了(我不在购物中心工作) 我的问题是,是否有一种从数据集中选择正确位置的好方法,如下所示: location | types MIT | 'school' western union | 'accounting', 'finance' Walmart | 'store',
上午和中午去学校,而在周末不去学校。
另一个,我在早上
时间去了购物中心,所以我可能今天和未来几天都不会再去那里了(我不在购物中心工作)
我的问题是,是否有一种从数据集中选择正确位置的好方法,如下所示:
location | types
MIT | 'school'
western union | 'accounting', 'finance'
Walmart | 'store', 'home_goods_store'
我知道每个地方的限制,这取决于:
一天中的时间
今天是工作日吗
我在那里多久了
访问频率
我今天去过那里吗?这个星期?这个月?
对于每个地方,都有不同的约束值组合
现在,我正在为每个约束使用许多字典,但使用如此多的if语句
有更好的方法吗?使用Python
编辑:
我的程序需要根据所有条件对每个活动进行分类
freq_dic['low_freq'] = ['airport','atm','bank','dentist','doctor','hospital','zoo','jewelry_store','spa']
freq_dic['high_freq'] = ['grocery_or_supermarket','gym','home_goods_store','primary_school','school',
'secondary_school','shopping_mall','store','supermarket','university']
time_dic['morning'] = ['airport', 'atm','bakery','bank','beauty_salon','bicycle_store','book_store','cafe',
'clothing_store','dentist','doctor','drugstore','gym','hair_care','home_goods_store',
'hospital','jewelry_store','library','local_government_office','pharmacy','primary_school',
'school','secondary_school','shopping_mall','spa','store','supermarket','university','zoo']
time_dic['noon'] = ['atm','bakery','bank','beauty_salon','bicycle_store','book_store','cafe','clothing_store',
'convenience_store','dentist','department_store','doctor','drugstore','grocery_or_supermarket',
'gym','hair_care','home_goods_store','hospital','jewelry_store','library','local_government_office',
'primary_school','restaurant','school','secondary_school','shopping_mall','spa','store',
'supermarket','university']
time_dic['evening'] = ['bakery','bar','bicycle_store','cafe','convenience_store','department_store','drugstore',
'grocery_or_supermarket','gym','home_goods_store','hospital','jewelry_store','movie_theater',
'night_club','restaurant','shopping_mall','store','supermarket']
visited_today_dic = {}
...
对于数据集中的每一行,我隔离了时间和频率,并将其发送到该函数:
def find_list(c_time, freq):
if 7 <= c_time <= 12:
if freq <= 6:
return list(set(time_dic['morning']).intersection(freq_dic['low_freq']))
elif freq <= 9:
return list(set(time_dic['morning']).intersection(freq_dic['med_freq']))
else:
return list(set(time_dic['morning']).intersection(freq_dic['high_freq']))
elif 12 < c_time <= 17:
if freq <= 6:
return list(set(time_dic['noon']).intersection(freq_dic['low_freq']))
elif freq <= 9:
return list(set(time_dic['noon']).intersection(freq_dic['med_freq']))
else:
return list(set(time_dic['noon']).intersection(freq_dic['high_freq']))
else:
if freq <= 6:
return list(set(time_dic['evening']).intersection(freq_dic['low_freq']))
elif freq <= 9:
return list(set(time_dic['evening']).intersection(freq_dic['med_freq']))
else:
return list(set(time_dic['evening']).intersection(freq_dic['high_freq']))
def find_列表(c_时间,频率):
if 7现在我用很多字典来处理每个约束,但是用这么多if语句看起来很糟糕代码在哪里?如果您的程序工作正常,这个问题可能属于代码检查,而不是堆栈溢出。我甚至不清楚你的程序要做什么。只是编辑并添加了一些代码。你已经添加了dict定义,没有任何关于你迄今为止尝试的逻辑。添加。对逻辑来说,就是在所有列表之间寻找公共值,这意味着它满足所有条件