30个Python优秀实践和技巧,你值得拥有~
| viewrawpillow.py hosted with ❤ by GitHub 
 除了显示图像,Pillow还可以分析、调整大小、过滤、增强、变形等等。有关它的所有特性,请参阅文档。 17. 使用map() Python的一个内置函数是map()。map()的语法是: map(function, something_iterable) 给定一个要执行的函数,和一些要运行的变量。它可以是任何可迭代的元素。在下面的例子中,我将使用一个列表。 defupper(s): 
 return s.upper() 
 mylist =list(map(upper, ['sentence', 'fragment'])) 
 print(mylist) 
 # ['SENTENCE', 'FRAGMENT'] 
 # Convert a string representation of 
 # a number into a list of ints. 
 list_of_ints =list(map(int, "1234567"))) 
 print(list_of_ints) 
 # [1, 2, 3, 4, 5, 6, 7] viewrawmap.py hostedwith ❤ by GitHub 看看自己的代码,看看是否可以在某处使用map()而不是循环! 18. 从列表和字符串中提取独特元素 通过使用set()函数创建一个集合,可以从一个列表或类似列表的对象中获得所有独特的元素: mylist = [1, 1, 2, 3, 4, 5, 5, 5, 6, 6] 
 print (set(mylist)) 
 # {1, 2, 3, 4, 5, 6} 
 # And since a string can be treated like a 
 # list of letters, you can also get the 
 # unique letters from a string this way: 
 print (set("aaabbbcccdddeeefff")) 
 (编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 

