可以用附属下表来取出人末尾向前的数组,不像其它语言需要先计算长度等等,例子如下:
>>> a = [1,2,3,4,5]
>>> print(a[-1])5
>>> print(a[-2])
4
>>>
统计数组a元素中出现次数
>>> a = [1,2,2,3,3,3,4,4,5]
>>> from collections import Counter
>>> print (Counter)
<class 'collections.Counter'>
>>> Counter(a)
#统计数组a中出现次数最多的元素,其中3出现3次,2出现2次,4出现2次,1出现1次,5出现1次
Counter({3: 3, 2: 2, 4: 2, 1: 1, 5: 1})
#统计数组a中出现最多的元素以及次数
>>> print(Counter(a).most_common(1))
[(3, 3)]
#统计数组a中出现最多的前两名的元素以及次数
>>> print(Counter(a).most_common(2))
[(3, 3), (2, 2)]
#统计数组a中元素以及出现的次数,大小排列
>>> print(Counter(a).most_common())
[(3, 3), (2, 2), (4, 2), (1, 1), (5, 1)]
>>>
print 输出 hello world ,一种直接用逗号连接,一种用加号直接连接
>>> print('lello' , ' world')
lello world
>>> print('lello' + ' world')
lello world
关于输入
a = input("请输入您的信息:")
print("My input is " , a)
============== RESTART: C:\Users\Administrator\Desktop\ceshi.py ==============
请输入您的信息:siyecao
My input is siyecao
>>>