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