我有两个较大的循环要处理,而我宁愿用Python理解列表。那可能吗?
参加:
void gl_mean (double x,double *count, double *mean) {
static double p;
static int timesIn = 0; //increment times in to update *count
timesIn++;
//p = x+(*mean * *count)/(*count+1);
p += x;
*count = (double)timesIn;
*mean = p/(*count);//*count must be greater than 0
//printf("%f\n",*mean);
}
int main (void) {
double num=0.0; //random number
double dI=1.0; //Counter - avoid div-by-zero,
double sum=0.0; //Sum
int i=0;
srand(time(0));
//for (gl_mean (num,i,sum);i<10;i++) {, this will not progress through the loops)
// because the initializer part of the for loop
// only gets called once
for (i=0;i<10;i++)//gl_mean (num,i,sum);i<10;i++) {
{
num=rand() %10 + 1; // "%10 + 1" optional to limit range from 1 to 10
gl_mean (num,&dI,&sum);//note the use of address operator to emulate
//passing a value by reference.
//values for dI and sum are updated in function
printf("%d) %f: %f\n",(int)dI, num, sum);
sum += num;
// i = (int)dI; //update loop index with updated value
}
}
我想要的是这样的
males = ['a', 'b']
females = ['c', 'd']
for i in males:
print(i)
for j in females:
print(j)
结果:
[print(i), print(j) for i, j in males, females]
答案 0 :(得分:2)
通常,将列表理解仅用于副作用不是一个好主意。参见this question。
我想说您的当前版本是最好的方法。但是,只要有可能...
for _ in map(print, males + females):
pass
或
[print(x) for x in males + females]
或者只是一个for循环
for x in males + females:
print(x)