我想知道是否有人可以帮我找出一种更有效的方法从用户输入的列表中选择一个索引这是我做的最好的方式,唯一的我能想到的另一种方法是用循环手动设置值。只是要注意那些print语句是在一个函数中,所以我可以折叠它们以使代码在IDE中更具可读性。这就是我所拥有的,任何反馈都将受到赞赏:
def LBMethodList():
print "Load balance Methods"
print "____________________"
print "1 - Round Robin"
print "2 - Ratio (member)"
print "3 - Least Connections (member)"
print "4 - Observed (member)"
print "5 - Predictive (member)"
print "6 - Ratio (node)"
print "7 - Least Connections (node)"
print "8 - Fastest (node)"
print "9 - Observed (node)"
print "10 - Predictive (node)"
print "11 - Dynamic Ratio (node)"
print "12 - Fastest (application)"
print "13 - Least Sessions"
print "14 - Dynamic Ratio (member)"
print "15 - Weighted Least Connections (member)"
print "16 - Weighted Least Connections (node)"
print "17 - Ratio (session)"
print "18 - Ratio Least Connections (member)"
print "19 - Ratio Least Connections (node)"
class LBMethodConvert:
LBMethodList()
ListLBMethod = ["LB_METHOD_ROUND_ROBIN", "LB_METHOD_RATIO_MEMBER", "LB_METHOD_LEAST_CONNECTION_MEMBER",
"LB_METHOD_OBSERVED_MEMBER", "LB_METHOD_PREDICTIVE_MEMBER", "LB_METHOD_RATIO_NODE_ADDRESS",
"LB_METHOD_LEAST_CONNECTION_NODE_ADDRESS", "LB_METHOD_FASTEST_NODE_ADDRESS",
"LB_METHOD_OBSERVED_NODE_ADDRESS", "LB_METHOD_PREDICTIVE_NODE_ADDRESS", "LB_METHOD_DYNAMIC_RATIO",
"LB_METHOD_FASTEST_APP_RESPONSE", "LB_METHOD_LEAST_SESSIONS", "LB_METHOD_DYNAMIC_RATIO_MEMBER",
"LB_METHOD_L3_ADDR", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_MEMBER",
"LB_METHOD_WEIGHTED_LEAST_CONNECTION_NODE_ADDRESS", "LB_METHOD_RATIO_SESSION",
"LB_METHOD_RATIO_LEAST_CONNECTION_MEMBER", "LB_METHOD_RATIO_LEAST_CONNECTION_NODE_ADDRESS"]
LBMethodSelect = input('Please select Load Balance method by number from list above: ')
MethodConstraint = LBMethodSelect - 1
LBMethod = ListLBMethod[MethodConstraint:LBMethodSelect]
答案 0 :(得分:2)
为什么不使用简单的索引,因为您只需要一个项目?相当肯定的切片效率低于索引:
LBMethodSelect = int(input('Please select Load Balance method by number from list above: '))
LBMethod = ListLBMethod[LBMethodSelect-1]
答案 1 :(得分:1)
__name__=='__main__'
list[x:x+1]
。只需访问list[x]
我做了一个小改写:
def LBMethodList():
print "Load balance Methods"
print "____________________"
print "1 - Round Robin"
print "2 - Ratio (member)"
print "3 - Least Connections (member)"
print "4 - Observed (member)"
print "5 - Predictive (member)"
print "6 - Ratio (node)"
print "7 - Least Connections (node)"
print "8 - Fastest (node)"
print "9 - Observed (node)"
print "10 - Predictive (node)"
print "11 - Dynamic Ratio (node)"
print "12 - Fastest (application)"
print "13 - Least Sessions"
print "14 - Dynamic Ratio (member)"
print "15 - Weighted Least Connections (member)"
print "16 - Weighted Least Connections (node)"
print "17 - Ratio (session)"
print "18 - Ratio Least Connections (member)"
print "19 - Ratio Least Connections (node)"
if __name__=='__main__':
LBMethodList()
ListLBMethod = ["LB_METHOD_ROUND_ROBIN", "LB_METHOD_RATIO_MEMBER", "LB_METHOD_LEAST_CONNECTION_MEMBER",
"LB_METHOD_OBSERVED_MEMBER", "LB_METHOD_PREDICTIVE_MEMBER", "LB_METHOD_RATIO_NODE_ADDRESS",
"LB_METHOD_LEAST_CONNECTION_NODE_ADDRESS", "LB_METHOD_FASTEST_NODE_ADDRESS",
"LB_METHOD_OBSERVED_NODE_ADDRESS", "LB_METHOD_PREDICTIVE_NODE_ADDRESS", "LB_METHOD_DYNAMIC_RATIO",
"LB_METHOD_FASTEST_APP_RESPONSE", "LB_METHOD_LEAST_SESSIONS", "LB_METHOD_DYNAMIC_RATIO_MEMBER",
"LB_METHOD_L3_ADDR", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_MEMBER",
"LB_METHOD_WEIGHTED_LEAST_CONNECTION_NODE_ADDRESS", "LB_METHOD_RATIO_SESSION",
"LB_METHOD_RATIO_LEAST_CONNECTION_MEMBER", "LB_METHOD_RATIO_LEAST_CONNECTION_NODE_ADDRESS"]
LBMethodSelect = input('Please select Load Balance method by number from list above: ')
LBMethod = ListLBMethod[LBMethodSelect-1]
print LBMethod
答案 2 :(得分:1)
要将数据保存在一起并在需要时将其扩展,您可以使用此示例:
LBMethods=[
("Round Robin", "LB_METHOD_ROUND_ROBIN"),
("Ratio (member)", "LB_METHOD_RATIO_MEMBER"),
("Least Connections (member)", "LB_METHOD_LEAST_CONNECTION_MEMBER"),
("Observed (member)", "LB_METHOD_OBSERVED_MEMBER"),
("Predictive (member)", "LB_METHOD_PREDICTIVE_MEMBER"),
("Ratio (node)", "LB_METHOD_RATIO_NODE_ADDRESS"),
("Least Connections (node)", "LB_METHOD_LEAST_CONNECTION_NODE_ADDRESS"),
("Fastest (node)", "LB_METHOD_FASTEST_NODE_ADDRESS"),
("Observed (node)", "LB_METHOD_OBSERVED_NODE_ADDRESS"),
("Predictive (node)", "LB_METHOD_PREDICTIVE_NODE_ADDRESS"),
("Dynamic Ratio (node)", "LB_METHOD_DYNAMIC_RATIO"),
("Fastest (application)", "LB_METHOD_FASTEST_APP_RESPONSE"),
("Least Sessions", "LB_METHOD_LEAST_SESSIONS"),
("Dynamic Ratio (member)", "LB_METHOD_DYNAMIC_RATIO_MEMBER"),
("Weighted Least Connections (member)", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_MEMBER"),
("Weighted Least Connections (node)", "LB_METHOD_WEIGHTED_LEAST_CONNECTION_NODE_ADDRESS"),
("Ratio (session)", "LB_METHOD_RATIO_SESSION"),
("Ratio Least Connections (member)", "LB_METHOD_RATIO_LEAST_CONNECTION_MEMBER"),
("Ratio Least Connections (node)", "LB_METHOD_RATIO_LEAST_CONNECTION_NODE_ADDRESS"),
]
def show_methods(method_list):
print "Load balance Methods"
print "____________________"
for index, method in enumerate(method_list):
print "%d - %s"%(index+1, method[0])
if __name__=='__main__':
show_methods(LBMethods)
index = input('Please select Load Balance method by number from list above: ')
print LBMethods[index-1][1]