我想根据操作有效地填充二维numpy数组
我想在一行中执行以下操作
1DArray = ['A','B','C','D']
N = len(1DArray)
2DArray = np.zeros((N,N))
for i in range(N):
for j in range (i, N):
2DArray[i,j] = function(1DArray[i], 1DArray[j])
答案 0 :(得分:0)
2DArray = [[0 for _ in range(i)] + [function(1DArray[i], 1DArray[j]) for j in range(i,N)] for i in range (N)]
[0 for _ in range(i)]
在未受影响的位置加0。您可以删除该列表以获取长度为N-j
答案 1 :(得分:0)
您可以使用numpy.triu_indices
public class Distribution100
{
public static void main(String[] args)
{
int numberOfArgs = Integer.parseInt(args[0]); // Integer n is equal to command line arguments.
int n[] = new int[numberOfArgs];
int[] array = new int[numberOfArgs]; // Establish new array and fill with integers from command line argument.
int low = 0; // Create integer low and set it to zero. This will be used later to count up based on the Command Line Argument values.
int high = 0; // create integer high and set it to zero. This will be used later to count up based on the Command Line Argument values.
for(int i = 0; i < numberOfArgs; i++)
/*For loop to read through the array and then follow the below steps. "args.length" will run the loop based on how many arguments are entered in the command line argument. */
{
array[i] = Integer.parseInt(args[i]);
if (array[i] >= 1 && array[i] <= 50) // if the number in the array falls between 1 and 50 do the next step
{
low++; // Increase low integer count by 1
}
else if (array[i] > 50 && array[i] <= 100) // If the number in the array falls between 50 and 100 do the next step
{
high++; // Increase low integer count by 1
}
}
System.out.println(low + " Numbers are less than or equal to 50.");
System.out.println(high + " Numbers are higher than 50.");
}
}