1.sort()和sorted()的区别2.怎么通过reverse参数对序列进行降序排列3.numpy怎么把一维数组变成二维数组4.快速插入元素到列表头部5.字典的创建方法6.通过一次查询给字典里不存在的键赋予新值7.统计字符串中元素出现的个数8.列表去重9.求m中元素在n中出现的次数10.新建一个Latin-1字符集合,该集合里的每个字符的Unicode名字里都有SIGN这个单词,用集合推导式完成。
1.sort()和sorted()的区别
l = [1, 9, 5, 8]
j = l.sort()
k = sorted(l)
sort()会在原序列上排序,sorted()新建了一个新的序列
2.怎么通过reverse参数对序列进行降序排列reverse一般放在sorted()方法里面,reverse默认值位False,
序列默认升序排列,降序排列的话需要将reverse值设置为Truel = [1, 9, 5, 8]
j = sorted(l, reverse=True)
print(j)
[9, 8, 5, 1]
3.numpy怎么把一维数组变成二维数组a = numpy.arange(12)
print(a)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
a.shape = 3, 4
print(a)
array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
4.快速插入元素到列表头部l = [1, 2, 3, 4, 5]
l[0:0] = 'Python'
print(l)
['P', 'y', 't', 'h', 'o', 'n', 1, 2, 3, 4, 5]l = [1, 2, 3, 4, 5]
l.insert(0, 'first')
print(l)
['first', 1, 2, 3, 4, 5]from collections import deque
dp = deque(range(10), maxlen=15)
print(dq)
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=15)
dp.appendleft(-1)
print(dq)
deque([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], maxlen=15)
5.字典的创建方法a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('tow', 2), ('one', 1), ('three', 3)])
e = dict({'one': 1, 'two': 2, 'three': 3})
6.通过一次查询给字典里不存在的键赋予新值coutry_code = {'China': 86, 'India': 91, 'US': 1, 'Brazil': 55,
'Russia': 7, 'Japan': 81}
coutry_code.setdefault('china', []).append(86)
if 'china' not in coutry_code:
coutry_code['china'] = []
coutry_code['china'].append(86)
print(coutry_code)
7.统计字符串中元素出现的个数import collections
c = collections.Counter('adcfadcfgbsdcv')
print(c)
Counter({'d': 3, 'c': 3, 'a': 2, 'f': 2, 'g': 1, 'b': 1, 's': 1, 'v': 1})
//统计排名前n的元素
print(c.most_common(2))
[('d', 3), ('c', 3)]
8.列表去重l = [1,2,3,4,5,6,6,5,4,3,2,1]
//使用字典的fromkeys()和keys方法
//fromkeys()方法去重,去重后获得元素为键,值为None的字典
//key()方法,返回以字典的键为元素的类列表
d = {}
l = d.fromkeys(l)
print(l)
{1: None, 2: None, 3: None, 4: None, 5: None, 6: None}
l = l.keys()
print(l)
dict_keys([1, 2, 3, 4, 5, 6])
l = list(l)
print(l)
[1, 2, 3, 4, 5, 6]//使用set()函数
//set()函数创建一个无序不重复元素集,可以删除重复数据
l2 = [1,2,3,4,5,6,6,5,4,3,2,1]
l2 = set(l2)
print(l2)
{1, 2, 3, 4, 5, 6}
l2 = list(l2)
print(l2)
[1, 2, 3, 4, 5, 6]//使用for循环
l3 = [1,2,3,4,5,6,6,5,4,3,2,1]
l4 = []
for x in l3;
if x not in l4:
l4.append(x)
print(l4)
[1, 2, 3, 4, 5, 6]
9.求m中元素在n中出现的次数m = {'A', 'B', 'C'}
n = {'B', 'C', 'D'}
find = 0
for i in m:
if i in n:
find += 1
print(find)
2m = {'A', 'B', 'C'}
n = {'B', 'C', 'D'}
print(len(m & n))
2
10.新建一个Latin-1字符集合,该集合里的每个字符的Unicode名字里都有SIGN这个单词,用集合推导式完成。from unicodedata import name
print({chr(i) for i in range(32, 256) if 'SIGN' in name(chr(i), '')})
{'®', '±', '×', '>', '÷', '+', '¶', '<', '$', '©', '=', '%', '#', '§', '°', '¥', '¢', '¬', '£', '¤', 'µ'}
原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说
免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。
您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com未经允许不得转载:书荒源码源码网每日更新网站源码模板! » Python技巧100题(七)
关注我们小说电影免费看关注我们,获取更多的全网素材资源,有趣有料!120000+人已关注
评论抢沙发