小编Pav*_*van的帖子

更改一个列表中的元素会更改多个列表

我有一份清单说mysolution:

>>>mySolution
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> mySolution[0][0] = 1    
>>> mySolution
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)

预期产量:

[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)

为什么我的列表列表中的所有第一个元素都被更改为1?我只想将第一个列表的第一个元素更改为1.

python list python-2.7

13
推荐指数
2
解决办法
8616
查看次数

在python re.findall中使用多个标志

我想在re.findall函数中使用多个标志.更具体地说,我想同时使用IGNORECASEDOTALL标志.

x = re.findall(r'CAT.+?END', 'Cat \n eND', (re.I, re.DOTALL))
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    x = re.findall(r'CAT.+?END','Cat \n eND',(re.I,re.DOTALL))
  File "C:\Python27\lib\re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
  File "C:\Python27\lib\re.py", line 243, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Python27\lib\sre_compile.py", line 500, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Python27\lib\sre_parse.py", line 673, in parse
    p = _parse_sub(source, pattern, 0)
  File "C:\Python27\lib\sre_parse.py", line 308, in _parse_sub
    itemsappend(_parse(source, state)) …
Run Code Online (Sandbox Code Playgroud)

python regex python-2.7

13
推荐指数
3
解决办法
2万
查看次数

Python字典列表[int:tuple]总和

我有一个词典列表.每个Dictionary都有一个整数键和元组值.我想总结位于元组某个位置的所有元素.

例:

myList = [{1000:("a",10)},{1001:("b",20)},{1003:("c",30)},{1000:("d",40)}]
Run Code Online (Sandbox Code Playgroud)

我知道我可以这样做:

sum = 0
for i in myList:
    for i in myList:
        temp = i.keys()
        sum += i[temp[0]][1]
print sum
Run Code Online (Sandbox Code Playgroud)

有更多的pythonic方式吗?谢谢

python

7
推荐指数
1
解决办法
442
查看次数

打印出指针指向的值(C编程)

我想打印指针指向的内容.这是我的代码:

int main(){
    int* pt = NULL;
    *pt = 100;
    printf("%d\n",*pt);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个分段错误.为什么?

c pointers segmentation-fault

4
推荐指数
2
解决办法
5万
查看次数

函数指针C中的结构

typedef struct Item{

  int i;
  int j;    
  void (*fooprint)(item*);

}item;

void fooprint(item *it){
  printf("%d\n",it.i);
}


int main(){

  item myitem;
  myitem.i=10;
  myitem.j=20;
  myitem.fooprint = fooprint;

  myitem.fooprint(&myitem);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码在void(footprint)(item)中给出错误."错误是预期的')'''''令牌"之前.我错过了什么吗?当我这样做而不使用指针结构是有效的.示例:void(*footprint)(item)

c pointers structure function

1
推荐指数
2
解决办法
5615
查看次数

从函数返回指针(变量在函数中声明)

int* sum(int *mypt,int len){
int i;
int mysum = 0; 
for(i=0;i<len;i++)
  mysum += mysum;   
  return &mysum; // I cannot do this since the function frame goes away after the call.
}

int main()
{

  int *pt = (int*) malloc(3*sizeof(int));   
  pt[0] = 0;
  pt[1] = 1;
  pt[2] = 2;

  int *s = sum(pt,3);
  printf("%d\n",*s);    
  return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我想返回指针mysum.我不能这样做,int static sum = 0因为它是固定的.我无法使用,int const sum = 0因为它给出了一个错误""readonly"".这个问题有方法解决吗 ?

c pointers function return-type

1
推荐指数
1
解决办法
78
查看次数