Beginning Python - Chapter2 : Lists and Tuples

------------chapter2:

----------1 six built-in types of sequences
list
tuples
strings
unicode strings
buffer objects
xrange objects

----------2 list VS tuples
list can be changed
tuples can not

----------3 container = sequences & mapping
the elements of a sequence are numbered, each element in a mapping
has a name (also called a key).

----------4 common sequence operation
indexing,slicing,adding,multiplying,check for membership;other build-in functions(find largest and smallest)-- iteration

--------indexing
>>> str="hello"
>>> str
'hello'
>>> str[0]
'h'
>>> str[-1]
'o'
>>> "hello"[1]
>>> str = raw_input("year: ")[3]
year: 2012
>>> str
'2'
------
months=['January','Feb','March']
year = raw_input("year: ")
month = raw_input("month: ")
day = raw_input("day: ")
print year + ","+ months[int(month)-1] +","+ day
>>>
year: 2012
month: 2
day: 12
2012,Feb,12
>>>
--------slicing (from begin to end)
>>> num=[0,1,2,3,4,5,6]
>>> num[1:6]
[1, 2, 3, 4, 5]
>>> num[4:-1]
[4, 5]
>>> num[-1:2]
[]
>>> num[-1:-3]
[] 
>>> num[-3:-1]
[4, 5]
------nifty shortcut
>>> num[0,7]
wrong, the range is between 0~6
>>> num[0:]
[0, 1, 2, 3, 4, 5, 6]
>>> num[:3]
[0, 1, 2]
>>> num[:]
[0, 1, 2, 3, 4, 5, 6]
------longer step
>>> num[3:1]
[]
>>> num[3:1:-1]
[3, 2]
>>> num[3:1,1]
[]
>>> num[::2]
[0, 2, 4, 6]
--------add sequences
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> "hello"+"world"
'helloworld'
--------Multiplication
>>> "hi"*2
'hihi'
>>> [2]*2
[2, 2]
--------None, Empty Lists, and Initialization
>>> num=[0]*10
>>> print num
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> num=[None]*10
>>> print num
[None, None, None, None, None, None, None, None, None, None]
--------check for Membership
>>> 'n' in 'no'
True
>>> 1 in 12
wrong
>>> num=[1,2]
>>> 1 in num
True
>>> name=['lili','lucy']
>>> lili in name
>>> raw_input("enter your name: ") in name
enter your name: lili
True
------
database=[['lili','01'],['lucy','02']]
name=raw_input("name: ")
no=raw_input("no: ")
if [name,no] in database : print 'you are in'
else : print 'you are not in'

>>>
name: lili
no: 01
you are in
>>>
name: lili
no: 02
you are not in
--------Length, Minimum, and Maximum
note:int has no len()
>>> num=[1,2,3]
>>> len(num)
3
>>> min(num)
1
>>> max(num)
3
>>> string=['a','b']
>>> len(string)
2
>>> max(string)
'b'
>>> min(string)
'a'
----------5 List
--------Basic List Operations
all sequences' operations ;
change a list methods(item assignments,item deletion,slice assignments,list method);
note: not all list methods actually change this list
--------item assignments
>>> num=[1,1,1]
>>> num[1]=2
>>> num
[1, 2, 1]

--------delete elements
>>> name=['lili','lucy']
>>> del name[0]
>>> name
['lucy']

--------assign to slices
note:not replace current loaction
>>> name=list('lili')
>>> name[2:]=list('sa')
>>> name
['l', 'i', 's', 'a']

note:can be different length
>>> language=list('perl')
>>> language[1:]=list('ython')
>>> language
['p', 'y', 't', 'h', 'o', 'n']

note:without replace any of original ones
>>> num=[1,5]
>>> num[1:1]=list('234')
>>> num
[1, '2', '3', '4', 5]

note:delete
>>> num
[1, '2', '3', '4', 5]
>>> num[1:4]=[]
>>> num
[1, 5]

--------list methods
object.method(arguments)

------append
>>> num=[1,2,3]
>>> num.append(4)
>>> num
[1, 2, 3, 4]

>>> name=['lili']
>>> name.append('lucy')
>>> name
['lili', 'lucy']

------extend
>>> num1=[1,2,3]
>>> num2=[4,5,6]
>>> num1.extend(num2)
>>> num1
[1, 2, 3, 4, 5, 6]

familiar with ,but the previous one(extend) is more efficient
a=a+b
like
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a[len(a)-1:]=b
>>> a
[1, 2, 4, 5, 6]

------count
note:count mount
>>> [1,1,3,[1,1]].count([1,1])
1
>>> [1,1,3,[1,1]].count(1)
2

------index
>>> name=['lili','lucy','xuehai']
>>> name.index('lili')
0
>>> name.index('he')
wrong,he is not in list

------insert
>>> num=[1,2,4]
>>> num.insert(1,'hi')
>>> num
[1, 'hi', 2, 4]

same with ,the previous one more clear
>>> num[1:1]=['hi']
>>> num
[1, 'hi', 2, 4] 

------pop
the pop method removes an element(by default,the last one)from the list and returns it;
the pot method is the only method that both modifies the list and returns a value(other than None )
>>> num=[1,2,3,4]
>>> num.pop()
4
>>> num
[1, 2, 3]
>>> num.pop(0)
1
>>> num
[2, 3]

------remove
the remove method is used to remove the first occurrence of a value.
>>> x=['to','be','to']
>>> x.remove('to')
>>> x
['be', 'to']

------reverse
note:the reverse method does not return anything
>>> x.reverse()
>>> x
['to', 'be']

>>> x=[1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]

reversed note:return iterate
>>> list(reversed(x))
[1, 2, 3]
>>> x
[3, 2, 1]

------sort
note:change the original list not return
>>> x=[1,5,3]
>>> x.sort()
>>> x
[1, 3, 5]

----sorted
note:This function can actually be used on any sequence, but will always return a list;
>>> y=sorted(x)
>>> y
[1, 3, 5]

>>> x=['hi','hello']
>>> sorted(x)
['hello', 'hi']
>>> sorted("hello")
['e', 'h', 'l', 'l', 'o']

----Advanced Sorting
--cmp
>>> cmp(2,3)
-1
>>> cmp(3,2)
1
>>> cmp(2,2)
0
--sort (two argument,key & reverse)
Question:other way using key & reverse
>>> x=['a','edf','gh']
>>> x.sort(key=len)
>>> x
['a', 'gh', 'edf']

>>> x.sort(reverse=True)
>>> x
['gh', 'edf', 'a']

----------6 tuples
note:just like lists,the only difference is that tuples can't be changed.

--------init
note: tuples may also be (and often are) enclosed in parentheses
   even a single value,have to  add a comma
>>> 1,2,3
(1, 2, 3)
>>> (1,2,3)
(1, 2, 3)
>>> ()
()
>>> 42,
(42,)
>>> (42,)
(42,)
>>> 3*(40+2)
126
>>> 3*(40+2,)
(42, 42, 42)

--------Function
note:just like lists; take one sequence agument and revert to it a tuple
>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3))
(1, 2, 3)
>>> list('123')
['1', '2', '3']

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章