Exercise 34: Accessing Elements Of Lists

自己的几个list常用用法。

mylist = [i for i in range(10)]
mylist.pop(-1)
mylist.append(9)
mylist_1 = [i for i in range(10,20)]
mylist_2 = zip(mylist, mylist_1)mylist.extend(mylist_1)

不常用的extend(), 里面的对象是list.

filter, map, reduce对于可迭代对象的操作。

filter(bool_func,seq)

filter(lambda x : x%2 == 0,[1,2,3,4,5])

此函数的功能相当于过滤器。调用一个布尔函数bool_func来迭代遍历每个seq中的元素;返回一个使bool_seq返回值为true的元素的序列。

map(func,seq1[,seq2...])

 

将函数func作用于给定序列的每个元素,并用一个列表来提供返回值;如果func为None,func表现为身份函数,返回一个含有每个序列中元素集合的n个元组的列表。

reduce(func,seq[,init])

func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值:如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素。

zip([iterable, ...])

zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。利用*号操作符,可以将list unzip(解压),看下面

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b)

[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c)
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped)
[(1, 2, 3), (4, 5, 6)]
时间: 2024-12-18 02:08:34

Exercise 34: Accessing Elements Of Lists的相关文章

Python Index

Table Of ContentsThe Hard Way Is EasierExercise 0: The SetupExercise 1: A Good First ProgramExercise 2: Comments And Pound CharactersExercise 3: Numbers And MathExercise 4: Variables And NamesExercise 5: More Variables And PrintingExercise 6: Strings

笨方法学Python,Lesson 32 - Lesson 34

Exercise 32 代码 the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list for number in the_count:     print "This is count %d&q

Guava包学习---Lists

Guava包是我最近项目中同事推荐使用的,是google推出的库.里面的功能非常多,包括了集合.缓存.原生类型支持.并发库.通用注解.字符串处理.IO等.我们项目中使用到了guava依赖,但是实际上只是用了其中很小一部分功能,比如集合的声明和处理以及函数式风格等. 废话少说,上图先: 我们会发现里面太多的东西,基本上全部加起来得有数百个上千的类了,但是所经常使用的其实就几十个类.其实可以在项目中建立common-utils包专门抄一部分guava的类过去,而不必全部将guava依赖进来. 工作中

Exercise 3.4 A calculator that allows multiple calculations

Exercise 3-4. Modify the last example in the chapter that implemented a calculator so that the user is given the option to enter y or Y to carry out another calculation, and n or N to end the program. (note: You’ll have to use a goto statement for th

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

To solve the general programming problem, you need to create any number of objects, anytime, anywhere. So you can't rely on creating a named reference to hold each one of your objects. Java has several ways to hold objects: 1. the compiler-supported

why using List list = new ArrayList() better than Arraylist list?

This is called programming to interface. This will be helpful in case if you wish to move to some other implementation of List in the future. If you want some methods in ArrayList then you would need to program to the implementation that is ArrayList

TIJ英文原版书籍阅读之旅——Chapter Eleven:Holding Your Objects

Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It holds objects of a known type so that you don't have to cast the result when you're looking up an object. It can be multidimensional, and it can hold

JDK源码Collections

此类完全由在 collection 上进行操作或返回 collection 的静态方法组成.它包含在 collection 上操作的多态算法,即"包装器",包装器返回由指定 collection 支持的新 collection,以及少数其他内容. 如果为此类的方法所提供的 collection 或类对象为 null,则这些方法都将抛出 NullPointerException. 此类中所含多态算法的文档通常包括对实现 的简短描述.应该将这类描述视为实现注意事项,而不是规范 的一部分.实