import numpy as np
import pandas as pd# pandas数据分析选则接近数值的最接优方案# 1.准备数据
CHILD_TABLE = (720, 750)
CHIDL_STOOL = (300, 350)
CHILD_PLAY_LEN = (300, 400)
CHILD_TENT = (1100, 1300)
# 2.遍历循环,添加到列表中
sum_length_lst = []
play_lst = []
table_lst = []
stool_lst = []
tent_lst = []
for play_b in CHILD_PLAY_LEN:for table_b in CHILD_TABLE:for stool_b in CHIDL_STOOL:for tent_b in CHILD_TENT:tent_lst.append(tent_b)play_lst.append(play_b)table_lst.append(table_b)stool_lst.append(stool_b)# 3.计算各个之和,生成列表sum_len = play_b + table_b + stool_b + tent_bsum_length_lst.append(sum_len)
# 4.添加到字典
pd_dict = {"sum_len": sum_length_lst,"play_len": play_lst,"table_len": table_lst,"stool_len": stool_lst,"tent_len": tent_lst
}
# 5.生成列表数据、排序
df = pd.DataFrame(pd_dict)
df = df.sort_values(by='sum_len')
print(df)
自动生成列表并排序:
# 6.根据判断距离寻找列表接近数值的表方案
dis = 2700
nearst_df = df.loc[df.sum_len <= dis ]
nearst_df = nearst_df.ix[nearst_df.sum_len.idxmax]
print(nearst_df)
结果显示如下: