nodes: 0, module; 1, function_definition; 2, function_name:sort_shell; 3, parameters; 4, block; 5, identifier:shell; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, comment:# Transpose of coefficients; 10, expression_statement; 11, comment:# For each primitive, find the index of the first nonzero coefficient; 12, expression_statement; 13, comment:# Zip together exponents and coeffs for sorting; 14, expression_statement; 15, comment:# Sort by decreasing value of exponent; 16, expression_statement; 17, comment:# Now (stable) sort by first non-zero coefficient; 18, expression_statement; 19, comment:# Unpack, and re-transpose the coefficients; 20, expression_statement; 21, expression_statement; 22, comment:# Now sort the columns of the coefficient by index of first nonzero coefficient; 23, expression_statement; 24, expression_statement; 25, expression_statement; 26, expression_statement; 27, expression_statement; 28, expression_statement; 29, return_statement; 30, identifier:use_copy; 31, True; 32, comment:"""
      Sort a basis set shell into a standard order
  
      If use_copy is True, the input shells are not modified.
      """; 33, identifier:use_copy; 34, block; 35, assignment; 36, assignment; 37, assignment; 38, assignment; 39, assignment; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, assignment; 45, assignment; 46, assignment; 47, assignment; 48, identifier:shell; 49, expression_statement; 50, identifier:tmp_c; 51, call; 52, identifier:nonzero_idx; 53, list_comprehension; 54, identifier:tmp; 55, call; 56, identifier:tmp; 57, call; 58, identifier:tmp; 59, call; 60, identifier:tmp_c; 61, list_comprehension; 62, subscript; 63, list_comprehension; 64, identifier:tmp_c; 65, call; 66, identifier:nonzero_idx; 67, list_comprehension; 68, identifier:tmp; 69, call; 70, identifier:tmp; 71, call; 72, identifier:tmp_c; 73, list_comprehension; 74, subscript; 75, identifier:tmp_c; 76, assignment; 77, identifier:list; 78, argument_list; 79, call; 80, for_in_clause; 81, identifier:zip; 82, argument_list; 83, identifier:sorted; 84, argument_list; 85, identifier:sorted; 86, argument_list; 87, subscript; 88, for_in_clause; 89, identifier:shell; 90, string; 91, subscript; 92, for_in_clause; 93, identifier:list; 94, argument_list; 95, call; 96, for_in_clause; 97, identifier:zip; 98, argument_list; 99, identifier:sorted; 100, argument_list; 101, subscript; 102, for_in_clause; 103, identifier:shell; 104, string; 105, identifier:shell; 106, call; 107, call; 108, identifier:next; 109, argument_list; 110, identifier:c; 111, identifier:tmp_c; 112, subscript; 113, identifier:tmp_c; 114, identifier:nonzero_idx; 115, identifier:tmp; 116, keyword_argument; 117, identifier:tmp; 118, keyword_argument; 119, identifier:x; 120, integer:1; 121, identifier:x; 122, identifier:tmp; 123, string_content:exponents; 124, identifier:x; 125, integer:0; 126, identifier:x; 127, identifier:tmp; 128, call; 129, identifier:next; 130, argument_list; 131, identifier:c; 132, identifier:tmp_c; 133, identifier:tmp_c; 134, identifier:nonzero_idx; 135, identifier:tmp; 136, keyword_argument; 137, identifier:x; 138, integer:0; 139, identifier:x; 140, identifier:tmp; 141, string_content:coefficients; 142, attribute; 143, argument_list; 144, identifier:map; 145, argument_list; 146, generator_expression; 147, None; 148, identifier:shell; 149, string; 150, identifier:key; 151, lambda; 152, identifier:key; 153, lambda; 154, identifier:map; 155, argument_list; 156, generator_expression; 157, None; 158, identifier:key; 159, lambda; 160, identifier:copy; 161, identifier:deepcopy; 162, identifier:shell; 163, identifier:list; 164, call; 165, identifier:i; 166, for_in_clause; 167, if_clause; 168, string_content:exponents; 169, lambda_parameters; 170, unary_operator; 171, lambda_parameters; 172, call; 173, identifier:list; 174, call; 175, identifier:i; 176, for_in_clause; 177, if_clause; 178, lambda_parameters; 179, call; 180, identifier:zip; 181, argument_list; 182, pattern_list; 183, call; 184, comparison_operator:float(x) != 0.0; 185, identifier:x; 186, call; 187, identifier:x; 188, identifier:int; 189, argument_list; 190, identifier:zip; 191, argument_list; 192, pattern_list; 193, call; 194, comparison_operator:float(x) != 0.0; 195, identifier:x; 196, identifier:int; 197, argument_list; 198, list_splat; 199, identifier:i; 200, identifier:x; 201, identifier:enumerate; 202, argument_list; 203, call; 204, float:0.0; 205, identifier:float; 206, argument_list; 207, subscript; 208, list_splat; 209, identifier:i; 210, identifier:x; 211, identifier:enumerate; 212, argument_list; 213, call; 214, float:0.0; 215, subscript; 216, subscript; 217, identifier:c; 218, identifier:float; 219, argument_list; 220, subscript; 221, identifier:x; 222, integer:2; 223, identifier:tmp_c; 224, identifier:c; 225, identifier:float; 226, argument_list; 227, identifier:x; 228, integer:1; 229, identifier:shell; 230, string; 231, identifier:x; 232, identifier:x; 233, integer:0; 234, identifier:x; 235, string_content:coefficients
code: def sort_shell(shell, use_copy=True):
      """
      Sort a basis set shell into a standard order
  
      If use_copy is True, the input shells are not modified.
      """
  
      if use_copy:
          shell = copy.deepcopy(shell)
  
      # Transpose of coefficients
      tmp_c = list(map(list, zip(*shell['coefficients'])))
  
      # For each primitive, find the index of the first nonzero coefficient
      nonzero_idx = [next((i for i, x in enumerate(c) if float(x) != 0.0), None) for c in tmp_c]
  
      # Zip together exponents and coeffs for sorting
      tmp = zip(shell['exponents'], tmp_c, nonzero_idx)
  
      # Sort by decreasing value of exponent
      tmp = sorted(tmp, key=lambda x: -float(x[0]))
  
      # Now (stable) sort by first non-zero coefficient
      tmp = sorted(tmp, key=lambda x: int(x[2]))
  
      # Unpack, and re-transpose the coefficients
      tmp_c = [x[1] for x in tmp]
      shell['exponents'] = [x[0] for x in tmp]
  
      # Now sort the columns of the coefficient by index of first nonzero coefficient
      tmp_c = list(map(list, zip(*tmp_c)))
      nonzero_idx = [next((i for i, x in enumerate(c) if float(x) != 0.0), None) for c in tmp_c]
  
      tmp = zip(tmp_c, nonzero_idx)
      tmp = sorted(tmp, key=lambda x: int(x[1]))
      tmp_c = [x[0] for x in tmp]
  
      shell['coefficients'] = tmp_c
  
      return shell