上下文:我一直在學習Python幾個月,我能夠編寫能夠工作的代碼,但通常看起來非常難看,並且包含大量不必要的代碼。但由於我的知識有限,我通常很難找到更好的方法去做事。Python:從腳本中刪除冗餘行
我想就下面的代碼可以改進的一些建議。僅供參考 - 它工作得很好,但我猜測必須有方法來改進它。我知道我在循環中重複自己,例如?
def getTableGroupTopMember_Test1(coordinates):
'''
Depending on the number of coordinates given, this function/code returns a unique coordinate.
'''
mylist = coordinates
for adc in activeDataConnections:
if mylist.Count == 1:
for table in adc:
if table.Name == mylist[0]:
print "/"
elif mylist.Count == 2:
for table in adc:
if table.Name == mylist[0]:
for topgroup in table.TopAxis.Groups:
if topgroup.Name == mylist[1]:
print topgroup.Address
elif mylist.Count == 3:
for table in adc:
if table.Name == mylist[0]:
for topgroup in table.TopAxis.Groups:
if topgroup.Name == mylist[1]:
for topmember in topgroup:
if topmember.Name == mylist[2]:
print topmember.Address
else:
print "your code is shit!"
getTableGroupTopMember_Test1(["Table10"])
getTableGroupTopMember_Test1(["Table10", "profile_julesage",])
getTableGroupTopMember_Test1(["Table10", "profile_julesage", "_25_to_34"])
輸出:
/
/2
/2[3]
謝謝大家!
這應該是http://codereview.stackexchange.com/填寫。該網站的設立是爲了改進工作代碼。 – iCodez
不知道,非常感謝! –