Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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:

  1. 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.
    
  2. 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.

  3. 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.

  4. Using print(). Most of the time instead of using simple print() 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:

  1. 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.
    
  2. 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.

  3. 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.

  4. Using print(). Most of the time instead of using simple print() 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:

  1. 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.
    
  2. 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.

  3. 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.

  4. Using print(). Most of the time instead of using simple print() 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' ... )
    
Source Link
BeetDemGuise
  • 4.2k
  • 12
  • 29

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:

  1. 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.
    
  2. 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.

  3. 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.

  4. Using print(). Most of the time instead of using simple print() 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' ... )
    
lang-py

AltStyle によって変換されたページ (->オリジナル) /