❶ python 如何计算一个数组所占的内存
list类似于 Vector
对象和指针数组是分开分配的,数组是在堆上。指针数组的大小是动态分配的,分配的内存肯定要多于实际的。既然是动态分配的,那么realloc调整大小就会移动数据,复制数据,大量数据还是用链表比较好。
dict类似于 Hash table
字典本身默认有元素容量,不够的才去堆上分配。需要扩容或者收缩,就会动态重新分配内存,重新hash。dict的keys()等调用生成list 如果数量很大,建议用迭代器。
❷ 请教各位,如何得到一个PYTHON变量的字节大小
python获得一个变量的名字的方法:
正常人:
items={
'tom':[1,2,3],
'jack':[4,5,6],
'alice':[7,8,9]
}
for x in items:
print "id of %s is %d" %(x,id(x))
变态:
tom = [1,2,3]
jack = [4,5,6]
alice = [7,8,9]
items = tom, jack, alice
for item in items:
for itemx,itemy in locals().items():
if item == itemy and itemx != 'item':
print itemx
❸ python列表值是怎么比较大小的
肯定不是相加
但有可能是从左到右依次比较(前面都相等再比较下一个)
话说回来list比较没意义,如果你想要有意义的结果,可以自定义类型,并重写比较的方法
❹ python中list的大小最大是多少
一般应用场景都不用考虑这个大小,因为这个上限很高,需要用到这么多元素的list的时候,都需要考虑很多其它问题。
1,32位python的限制是 536870912 个元素。
2,64位python的限制是 1152921504606846975 个元素。
❺ python怎么查看list长度
L=[1,2,6,8,7]
len(L)
长度为5
使用len()查看
❻ python写一个文件 记录一个list里数字的大小位置 从大到小 不能用内置函数和sorted
根据列表的长度n生成一个0到n序列号的列表。
然后使用冒泡排序的方法同时对两个列表排序,排序完成后,序列列表就是你要的。
至于写入文件,可以使用open按一般文件内容写入。
但保存的只是一个列表变量,适合使用shelve来保存为data文件,所以使用shelve.open来保存数据。
import shelve
def orderIndex(nums=[]):
n=len(nums)
index=0
indexs=[]
for i in range(n):
indexs.append(index)
index=index+1
for i in range(n):
for j in range(i+1,n):
if(nums[i]<nums[j]):
nSave=nums[i]
nums[i]=nums[j]
nums[j]=nSave
iSave=indexs[i]
indexs[i]=indexs[j]
indexs[j]=iSave
return indexs
print("请输入一列数字,逗号分割 :")
nums=input().split(',')
print("从大到小排序后的序列为 :")
indexs=orderIndex(nums)
print(indexs)
shelFile=shelve.open('C:mydata')
shelFile['indexs']=indexs
shelFile.close()
print("序列已保存至文件 :C:mydata中")
❼ python list 比较大小问题
我觉得应该是 a.sort(cmp=lambda x,y: cmp(x[3],y[3])) 是in place 排序
下面是联机文档的内容:
The sort() and reverse() methods modify the list in place for economy of
space when sorting or reversing a large list. To remind you that they operate by
side effect, they don’t return the sorted or reversed list.
The sort() method takes optional arguments for controlling the
comparisons.cmp specifies a custom comparison function of two arguments (list
items) which should return a negative, zero or positive number depending on
whether the first argument is considered smaller than, equal to, or larger than
the second argument: cmp=lambdax,y:cmp(x.lower(),y.lower()). The
default value is None.key specifies a function of one argument that is used to extract a
comparison key from each list element: key=str.lower. The default value is None.reverse is a boolean value. If set to True, then the list
elements are sorted as if each comparison were reversed.In general, the key and reverse conversion processes are
much faster than specifying an equivalent cmp function. This is because
cmp is called multiple times for each list element while key
and reverse touch each element only once. Use functools.cmp_to_key() to convert an old-style
cmp function to a key function.Changed in version 2.3:
Support for None as an equivalent to omitting cmp was
added.Changed in version 2.4:
Support for key and reverse was added.
Starting with Python 2.3, the sort() method is
guaranteed to be stable. A sort is stable if it guarantees not to change the
relative order of elements that compare equal — this is helpful for sorting in
multiple passes (for example, sort by department, then by salary grade).
CPython implementation detail: While a list is being sorted,
the effect of attempting to mutate, or even inspect, the list is undefined. The
C implementation of Python 2.3 and newer makes the list appear empty for the
ration, and raises ValueError if it
can detect that the list has been mutated ring a sort.
❽ python怎样获取list的大小
一般应用场景都不用考虑这个大小,因为这个上限很高,需要用到这么多元素的list的时候,都需要考虑很多其它问题。
32位python的限制是 536870912 个元素。
64位python的限制是 1152921504606846975 个元素。
❾ python如何统计列表的长度
参考代码:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c"];
len(list1)
len(list2)
len(list3)
Python支持列表切割(list slices),可以取得完整列表的一部分。支持切割操作的类型有str, bytes, list, tuple等。它的语法是...[left:right]或者...[left:right:stride]。假定nums变量的值是[1, 3, 5, 7,],那么下面几个语句为真:
nums[2:5] == [5, 7] 从下标为2的元素切割到下标为5的元素,但不包含下标为5的元素。
nums[1:] == [3, 5, 7] 切割到最后一个元素。
nums[:-3] == [1, 3, 5, 7] 从最开始的元素一直切割到倒数第3个元素。
nums[:] == [1, 3, 5, 7] 返回所有元素。改变新的列表不会影响到nums。
nums[1:5:2] == [3, 7] 从下标为1的元素切割到下标为5的元素但不包含下标为5的元素,且步长为2。
(9)python查看list字节大小扩展阅读:
Python 是一门有条理的和强大的面向对象的程序设计语言,类似于Perl, Ruby, Scheme, Java。
Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读、易维护,并且被大量用户所欢迎的、用途广泛的语言。
设计者开发时总的指导思想是,对于一个特定的问题,只要有一种最好的方法来解决就好了。这在由Tim Peters写的Python格言(称为The Zen of Python)里面表述为:There should be one-- and preferably only one --obvious way to do it. 这正好和Perl语言(另一种功能类似的高级动态语言)的中心思想TMTOWTDI(There's More Than One Way To Do It)完全相反。
Python的作者有意的设计限制性很强的语法,使得不好的编程习惯(例如if语句的下一行不向右缩进)都不能通过编译。其中很重要的一项就是Python的缩进规则。