Because @jonrsharp @jonrsharp commented much of the functional improvements, I'll speak on style improvements.
Firstly, take a look at PEP8, the official Python style guide. It holds a lot of beneficial information about how Python code should look-and-feel.
Now onto my comments:
Beware of simple comments. Comments help explain the more intricate lines of code and the reasons behind them. Comments like
#beginning of main program
offer little-to-no value in terms of comprehension.Conventional comments in Python have a space after the
#
and the first word (unless its an identifier) is capitalized:# This is a better-styled comment.
Put spaces after commas. Whenever you have a comma-separated list in code (bulk declarations, parameters, etc.) always but a single space after each comma.
Use
str.join
to make printing easier. Here is one of your print statements:print(i+1,'\t',centers[0,i],'\t',centers[1,i],'\t',centers[2,i],'\t',centers[3,i])
join
helps removes the repeated use of hard-coded values:print('\t'.join(str(val) for val in [i+1] + [centers[j, i] for j in range(4)])))
The above may seem more complex than the original version. Visually, it may be. However its more flexible and Pythonic.
Using
print()
. Most of the time instead of using simpleprint()
to print a blank line, you can append a newline onto another print statement:for i in range(deal_number): print(i+1,'\t',end='') for j in range(K): print(deal_cluster_matrix[i,j],'\t',end='') print() print() print('The total distance of the solution found is' ... )
becomes:
for i in range(deal_number): print('\n', i+1, '\t', end='') for j in range(K): print(deal_cluster_matrix[i,j], '\t', end='') print('\nThe total distance of the solution found is' ... )
Because @jonrsharp commented much of the functional improvements, I'll speak on style improvements.
Firstly, take a look at PEP8, the official Python style guide. It holds a lot of beneficial information about how Python code should look-and-feel.
Now onto my comments:
Beware of simple comments. Comments help explain the more intricate lines of code and the reasons behind them. Comments like
#beginning of main program
offer little-to-no value in terms of comprehension.Conventional comments in Python have a space after the
#
and the first word (unless its an identifier) is capitalized:# This is a better-styled comment.
Put spaces after commas. Whenever you have a comma-separated list in code (bulk declarations, parameters, etc.) always but a single space after each comma.
Use
str.join
to make printing easier. Here is one of your print statements:print(i+1,'\t',centers[0,i],'\t',centers[1,i],'\t',centers[2,i],'\t',centers[3,i])
join
helps removes the repeated use of hard-coded values:print('\t'.join(str(val) for val in [i+1] + [centers[j, i] for j in range(4)])))
The above may seem more complex than the original version. Visually, it may be. However its more flexible and Pythonic.
Using
print()
. Most of the time instead of using simpleprint()
to print a blank line, you can append a newline onto another print statement:for i in range(deal_number): print(i+1,'\t',end='') for j in range(K): print(deal_cluster_matrix[i,j],'\t',end='') print() print() print('The total distance of the solution found is' ... )
becomes:
for i in range(deal_number): print('\n', i+1, '\t', end='') for j in range(K): print(deal_cluster_matrix[i,j], '\t', end='') print('\nThe total distance of the solution found is' ... )
Because @jonrsharp commented much of the functional improvements, I'll speak on style improvements.
Firstly, take a look at PEP8, the official Python style guide. It holds a lot of beneficial information about how Python code should look-and-feel.
Now onto my comments:
Beware of simple comments. Comments help explain the more intricate lines of code and the reasons behind them. Comments like
#beginning of main program
offer little-to-no value in terms of comprehension.Conventional comments in Python have a space after the
#
and the first word (unless its an identifier) is capitalized:# This is a better-styled comment.
Put spaces after commas. Whenever you have a comma-separated list in code (bulk declarations, parameters, etc.) always but a single space after each comma.
Use
str.join
to make printing easier. Here is one of your print statements:print(i+1,'\t',centers[0,i],'\t',centers[1,i],'\t',centers[2,i],'\t',centers[3,i])
join
helps removes the repeated use of hard-coded values:print('\t'.join(str(val) for val in [i+1] + [centers[j, i] for j in range(4)])))
The above may seem more complex than the original version. Visually, it may be. However its more flexible and Pythonic.
Using
print()
. Most of the time instead of using simpleprint()
to print a blank line, you can append a newline onto another print statement:for i in range(deal_number): print(i+1,'\t',end='') for j in range(K): print(deal_cluster_matrix[i,j],'\t',end='') print() print() print('The total distance of the solution found is' ... )
becomes:
for i in range(deal_number): print('\n', i+1, '\t', end='') for j in range(K): print(deal_cluster_matrix[i,j], '\t', end='') print('\nThe total distance of the solution found is' ... )
Because @jonrsharp commented much of the functional improvements, I'll speak on style improvements.
Firstly, take a look at PEP8, the official Python style guide. It holds a lot of beneficial information about how Python code should look-and-feel.
Now onto my comments:
Beware of simple comments. Comments help explain the more intricate lines of code and the reasons behind them. Comments like
#beginning of main program
offer little-to-no value in terms of comprehension.Conventional comments in Python have a space after the
#
and the first word (unless its an identifier) is capitalized:# This is a better-styled comment.
Put spaces after commas. Whenever you have a comma-separated list in code (bulk declarations, parameters, etc.) always but a single space after each comma.
Use
str.join
to make printing easier. Here is one of your print statements:print(i+1,'\t',centers[0,i],'\t',centers[1,i],'\t',centers[2,i],'\t',centers[3,i])
join
helps removes the repeated use of hard-coded values:print('\t'.join(str(val) for val in [i+1] + [centers[j, i] for j in range(4)])))
The above may seem more complex than the original version. Visually, it may be. However its more flexible and Pythonic.
Using
print()
. Most of the time instead of using simpleprint()
to print a blank line, you can append a newline onto another print statement:for i in range(deal_number): print(i+1,'\t',end='') for j in range(K): print(deal_cluster_matrix[i,j],'\t',end='') print() print() print('The total distance of the solution found is' ... )
becomes:
for i in range(deal_number): print('\n', i+1, '\t', end='') for j in range(K): print(deal_cluster_matrix[i,j], '\t', end='') print('\nThe total distance of the solution found is' ... )