pythonsizeoflist
A. 为什么python的容器这么占内存
用getsizeof(a) 取到的只是a一个list的object的大小 实际的大小要加上a里的4000000个float和2000个list的大小 所以这个2000 * 2000的list大小应该是120多mb
gc也没什么好collect的
想要占的少一点 最简单的方法是 用Numpy
B. python用单链表写一个通讯录,包括添加,删除(可恢复),查找等基本功能
///////////list3.c实现链表的插入删除查找
#include
#include
#include
typedef
struct
LNode
//////////定义数据结构体
{
int
num;
char
name[20];
struct
LNode*
next;
}*Link;
///////////定义一个指针类型
typedef
struct
{
Link
head,tail;
int
len;
}LinkList;
LinkList
*gList;
void
MenuInfo();
void
InputData(LinkList
*mList);
void
OutputData(LinkList
*mList);
void
InsertData(LinkList
*mList,int
n);
Link
SearchNode(LinkList
*mList,int
n);
void
DeleteData(LinkList
*mList,int
n);
void
main()
{
int
_choice;
int
_quit=0;
int
n=0;
gList=(LinkList
*)malloc(sizeof(LinkList));
gList->head=gList->tail=NULL;
do
{
MenuInfo();
scanf("%d",&_choice);
switch(_choice)
C. 如何应用python求函数积分
typedef struct lista{
struct lista *next;
int data;
}list;
void insert(list *h);
void del(list *h);
int main()
{
int flag;
list *head=(list *)malloc(sizeof(list));
head->next=NULL;
while(1)
{
D. python,删除有序链表重复元素,为什么没通过
哎!你刚提问题没多久,我就开始写代码,写到现在,不采纳真的是太对不起我了...
因为你没有写出具体的线性表,所以我假设该线性表是需要手动输入的!
代码如下运行通过:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node,*Sqlist;
void IniList(Sqlist *L) /*初始化*/
{
*L=(Sqlist)malloc(sizeof(Node));
(*L)->next=*L;
}
void Create_cLinkList(Sqlist L) /*尾插法建立链表*/
{
Node *s;
int c;
int flag=1;
bool bl;
L->data=NULL;
L->next=NULL;
while(flag)
{
bl=scanf("%d",&c);
if(bl)
{
s=(Node *)malloc(sizeof(Node));
s->data=c;
s->next=L->next;
L->next=s;
}
else
{
flag=0;
}
}
}
void Treserve( Sqlist &L) /*比较链表中的每个数字,重复就删除*/
{
Node *p;
Node *s;
s=p=L->next;
while(p->next!=NULL)
{
p=s;
p=p->next;
if(s->data==p->data)
{
if(p->next==NULL)
s->next=NULL;
else
s->next=p->next;
}
else
{
s=p;
}
}
}
main()
{
Sqlist la;
Node *p;
Node *s;
IniList(&la);
printf("输入循环单链表A数据,按从小到大的顺序输入,输入$符号结束:\n");
Create_cLinkList(la);
Treserve( la);
s=la;
p=la->next;
while(s->next!=NULL) /*输出改变后的链表*/
{
printf("%d",p->data);
s=p;
p=p->next;
}
}
E. python实现删除重复行并计数
F. 由于c++中没有实现如python中的string.split()和list.length()的方法,所以我自己写了两个程序,但有问题
很简单的问题啊,你仔细想一下,python的split切分出来的字符串数组是什么类型呢?list类型的啊!比如
x="abc def hgij"
x.split() 返回 ['abc', 'def', 'hgij']
那在C++中我也会整一个std::list啊,
std::list<string&> mystrlist;
来了一个字符串x
string x="abc def hgij"
pos1=x.find_first_not_of( ' ', pos2 );
截取到一段字符串之后
string y = x.substr( pos1, pos2-pos1 )
插入到list里面
mystrlist.push_back( y )
完事之后就可以对mystrlist做你想做的操纵了
mystrlist.size()
G. Python中的返回值问题!!!
首先,代码有误,你想调用的应该是
sorted([4,3,2,1])
reversed([4,3,2,1])
然后,你所说的reversed的返回值类型也不正确。
sorted返回的是list,reversed返回的是iterator。
list你应该很熟悉了。iterator和list是完全不同的东西。简单的说iterator只是提供一个接口,每次迭代可以产生一个值,到没有值为止。iterator在很多语言里面都有实现。在python里面主要用在for循环和list comprehension。
iterator和list/tuple/dict/set等容器的关系:
1.python内置的容器类几乎都实现了iterator接口。
显式获取某个容器的iterator可以调用iter函数:
l = [1,2,3,4]
i = iter(l)
//现在i就是一个list iterator。可以用来遍历l这个list.
i.next() # 1
i.next() # 2
//每一个iterator都必须实现next方法。并且在没有元素时抛出StopIteration异常。
在for语句和list comprehension中,都是隐式调用了这个函数。所以可以直接
for obj in some_container:
pass
2.某些容器可以通过iterator进行初始化,比如list
l = [1,2,3,4]
i = iter(l)
l2 = list(i)
最后,没有列表和列表对象这种说法。这两者一般都是指列表对象(instance of the type list)。如果你是想说列表类(the list type)本身,可以这样得到:
type([])
或者
[].__class__
H. Python小问题
出现这个问题是因为索引出现了浮点数,不是索引允许的数据类型,可以验证一下
importnumpyasnp
y=np.zeros(shape=(1,5))
arr=[nforninnp.linspace(1,5,5)]
arr里存储的就是源代码中会用的索引,下图是结果
importnumpyasnp
y=np.zeros(shape=(1,5))
forninnp.int16(np.linspace(1,5,5)):
y[0,n-1]=n**2
print(y)
I. python二叉树算法
定义一颗二叉树,请看官自行想象其形状
class BinNode( ):
def __init__( self, val ):
self.lchild = None
self.rchild = None
self.value = val
binNode1 = BinNode( 1 )
binNode2 = BinNode( 2 )
binNode3 = BinNode( 3 )
binNode4 = BinNode( 4 )
binNode5 = BinNode( 5 )
binNode6 = BinNode( 6 )
binNode1.lchild = binNode2
binNode1.rchild = binNode3
binNode2.lchild = binNode4
binNode2.rchild = binNode5
binNode3.lchild = binNode6