假设我有一个包含 16 个数字的列表。有了这 16 个数字,我可以创建不同的 4x4 矩阵。我想找到所有 4x4 矩阵,其中列表中的每个元素都使用一次,并且每行和每列的总和等于 264。
首先,我找到列表中元素的所有组合,总和为 264
numbers = [11, 16, 18, 19, 61, 66, 68, 69, 81, 86, 88, 89, 91, 96, 98, 99]
candidates = []
result = [x for x in itertools.combinations(numbers, 4) if sum(x) == 264]
Run Code Online (Sandbox Code Playgroud)
result成为一个列表,其中每个元素都是一个包含 4 个元素的列表,其中 4 个元素的总和 = 264。我认为这些是我的行。然后我想对我的行进行所有排列,因为加法是可交换的。
for i in range(0, len(result)):
candidates.append(list(itertools.permutations(result[i])))
Run Code Online (Sandbox Code Playgroud)
现在给出总和为 264 的所有可能行。我想选择 4 行的所有组合,以便每列的总和为 264。
test = []
for i in range(0, len(candidates)):
test = test + candidates[i]
result2 = …Run Code Online (Sandbox Code Playgroud) python matrix combinatorics constraint-programming python-3.x
我有一个整数列表
keys = [18, 99, 86, 61, 66, 81, 98, 19, 91, 16, 69, 88, 89, 68, 11, 96]
Run Code Online (Sandbox Code Playgroud)
我想找到这个列表的所有排列,这样对于每个排列
元素 0 到 3 加起来为 264,
元素 4 到 7 加起来为 264,
元素 8 到 11 加起来为 264 和
元素 12 到 15 广告最多 264。
目前我有以下策略
使用 itertools.permutations 计算所有排列
检查哪些排列满足我的条件
是否有另一种性能更好的策略?
我有一个 DataFrame (带有日期时间索引)例如
2017-01-01 00:00:00 -8.64
2017-01-01 01:00:00 1.02
2017-01-01 02:00:00 1.03
2017-01-01 03:00:00 0.00
2017-01-01 04:00:00 -1.01
2017-01-01 05:00:00 -3.57
2017-01-01 06:00:00 -4.18
2017-01-01 07:00:00 7.73
Run Code Online (Sandbox Code Playgroud)
我想用绝对最大值将其重新采样到 4Hours,即结果应该是
2017-01-01 00:00:00 -8.64
2017-01-01 04:00:00 7.73
Run Code Online (Sandbox Code Playgroud)
但我找不到任何方法来做到这一点。我尝试了 df.resample('4H').max(key=abs)
我有一个任务,找到这个代码错误的原因.
#include <stdlib.h>
#include <stdio.h>
#define fail(a) ((test == 0 || test == a) ? fail##a() : 0)
#define N (10)
int a[N] = { 1 };
int* b = &a[0];
void fail1()
{
printf("a[0] = %d\n", a[0]);
printf("b[0] = %d\n", b[0]);
printf("*b = %d\n", *b);
*b = 2;
a[N] = 3;
printf("*b = %d\n", *b);
}
...
int main(int argc, char **argv)
{
int test = 0;
if (argc > 1) {
sscanf(argv[1], "%d", &test);
printf("doing test %d\n", test); …Run Code Online (Sandbox Code Playgroud) #运算符的目的是什么,称为字符串化运算符,它是如何使用的?
我的书以下面的方式描述#,光头部分是我不明白的.
字符串化器操作符#必须后跟一个参数,并且它们将替换为从参数的标记构造的字符串文字,这些字符串文字不会被首先替换.即,以下输入:
#define W 124
#define str(a) (#a)
str(W)
Run Code Online (Sandbox Code Playgroud)
产生输出:
"W"
如果我们想要一个带有宏定义的字符串文字,我们必须使用两个类似函数的宏:
#define W 124
#define xstr(a) (#a)
#define str(b) (xstr(b))
Run Code Online (Sandbox Code Playgroud)
产生输出
"124"
这样做的原因是参数b的参数在str的replacement-list中替换参数之前被完全替换,这意味着xstr的调用将使用124作为参数,然后在xstr中进行字符串化
在编译下面的程序时,终端给我以下消息"double free or corruption(out)".我想创建一个程序,首先计算数组中所有元素的总和,见下面的x.然后我想计算指针指向的内存块中所有数字的总和,见下面的y.我认为问题在于作业"y = x;"
int main(void)
{
double x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf("The sum is %f\n", sum(x, 10));
double *y = malloc(sizeof(double)*10);
y = x;
printf("The sum is %f\n", sum(y, 10));
free(y);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我学习C和尝试,我写了一个小程序.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int *x = malloc(sizeof(int)*3);
int i;
for(i=0;i<3; i++){
x[i] = i*i;
printf("x[i] = %d\n", x[i]);
}
free(x);
}
Run Code Online (Sandbox Code Playgroud)
现在输出是(ofc it)
x [i] = 0
x [i] = 1
x [i] = 4
我的问题是,我如何更改代码以获得输出?
x [0] = 0
x [1] = 1
x [2] = 4
c ×4
python ×3
arrays ×1
combinations ×1
matrix ×1
pandas ×1
permutation ×1
pointers ×1
printing ×1
python-3.x ×1
string ×1