- 在列表中查看相等数字的长度
def calnumber(a,k):#a为列表,k为查找数字
count=0
for i in a:
if i == k:
count = count + 1
else:
continue
return count
a=[1,2,3,4,1,3]
length=calnumber(a,1)
print(length)
- 使用set函数可去除列表中重复数字
a=[1,2,3,4,1,3]
print set(a)
- 输出单独数字的个数
a = [1,2,2,2,8,3,3,4,5,4,4]
b = set(a)
for item in b:
print("the %d has found %d Times" %(item,a.count(item)))
- 根据数据长度向列表中添加
a = [1,2,2,2,8,3,3,4,5,4,4]
b = set(a)
newlist=[]
for item in b:
length=0
length=a.count(item)
newlist=newlist+addnum_toList(length, item)
print(newlist)
- 使用operator.eq函数判断两个列表是否相等
a=[1,2,3,1,2]
b=[1,2,3,1,2]
c=[1,2,3]
print(operator.eq(a,b))#判断两个list是否相等
print(operator.eq(a,c))