【跟着MIT学Python】Unit 3.5 Tuples and Lists

Tuples

Definition

An ordered sequence of elements, can mix elements types

  • dentoed by ( )
te = () # an empty tuple
te = (2,"one",3)
in: te[0]
out: 2
(2,"one",3)+(5,6)
(2,"one",3,5,6)
in: te[1:2]
out: ("one",) 
# with a comma, indicates that this is a tuple insted of expression of single element

Charateristic:

  • immutable
te[1] = 4  # ---> this is an error
  • conveniently to swap variable values
(x,y) = (y,x)
  • return more than 1 value from a function
return (r,p)
  • can interate over tuples
def get_data(aTuple):
	nums = ()
	words = ()
	for t in aTuple:  # This expression is worktable
		nums = nums +(t[0],)
		if t[1] not in words:
			words = words + (t[1],) # HERE!!!! (t[1],) ,this comma is extremely important
	min_nums = min(nums)
	max_nums = max(muns)
	unique_words = len(words)
	return (min_nums, max_nums, unique_words)

Exercise

x = (1, 2, (3, 'John', 4), 'Hi')
# x is a tuple
# while for the sigle element in the tuple...
In x[0]
Out 1

In type(x[0])
Out int
# nested tuple 
In x[2]
Out (3, 'John', 4)

In type(x[2])
Out tuple

In x[2][2]
Out 4
# for the single element in tuple...
In x[-1][-1]
Out 'i'
# the length of a tuple
In len(x)
Out 4

Lists

definition

Ordered sequence of information, accessiable by index

x = [1,2,3,'Hi']

characteristic

  • mutable
L = [2,1,3]
In:  L[1] = 5
#  now L is [2,5,3]
  • index can be variable or expression
L[i-1] #expression
  • interation over the list
L = [1,2,3]
for i in L:
	print(L[i])

Exercise

x = [1, 2, [3, 'John', 4], 'Hi']
# Tho here is noly one element, still treated as a list
In x[0:1]
Out [1]

List operations

5. Data Structures - Python 3.10.2 documentation

  • ADD
methodappend(x)extend(interable)insert(index,item)
returnNoneNoneNone
  • Deletion
methodremove(x)pop ([i])del(L[i])clear
returnNonepopped itemNoneNone
noteremove the first item == x, Error is no such thingremove all
  • Sorted
methodL.sortL.sort()sorted(L)
returnfunctionNonesorted item
notemutates the list, returns nothingdoes not mutate the list, must assign to something

Mutation, Aliasing, Cloning

Aliasing

warm = ['orange','yellow','pink']
hot = warm # this two are aliased
hot[0] = 'red'
In: warm
Out: ['red','yellow','pink']
# pointing to the same object

在这里插入图片描述

Cloning

cool = ['blue','green','grey']
chill = cool[:]
chill.extend('black')
# changes in chill won't change cool
# they point to different objects

在这里插入图片描述

Mutation

  • Sorting list

Click here

List of list of list

warm = ['yellow','orange']
hot = ['red']
brightcolors = [warm]

center

brightcolors.append(hot)
In: print(brightcolors)
Out: [['yellow','orange'],['red']]

在这里插入图片描述

def remove_dups(l1,l2):
	for e in l1:
		if e in l2:
			 l1.remove(e)
In: l1 = [1,2,3,4]
		l2 = [1,2,4,5]
		remove_dups(l1,l2)
Out: [2,3,4]

💡 WHY 2 is not removed?
because Python uses an internal counter to keep track of index it is in the loop.
mutation changes the list length but Python doesn’t update the counter.

def remove_dups(l1,l2):
	l1_copy = l1[:]
	for e in l1_copy:
		if e in l2:
			 l1.remove(e)
In: l1 = [1,2,3,4]
		l2 = [1,2,4,5]
		remove_dups(l1,l2)
Out: [2,3,4]

Functions as objects,dictitionaries

def applyToEach(L,f):
	for i in range(len(L)):
		L[i] = f(L[i])

In: applyToEach(L,abs)
In: applyToEach(L,int)
In: applyToEach(L,fact)
def applyFuns(L,x):
	for f in L:
		print(f(x))

In: applyFuns([abs,int,fact,fib],4)
  • MAP

    simple form a unary function and collection of suitable arguements

    for elt in map(abs,[1,-2,3,-4])
    	print(elt)
    [1,2,3,4]
    

    general form - an n-ary function and n collections of arguments

    L1 = [1,28,36]
    L2 = [2,57,9]
    for elt in map(min,L1,L2)
    	print(elt) 
    # compare each elements in the N place of L1&L2
    [1,28,9]
    

    Review

    TypeType of elementsExample of literalsMutable
    strcharacters‘’,’a’,’abc’No
    tupleany type( ),(3,),(’abc’,4)No
    rangeintegersrange(10),range(1,10,2)No
    listany type[1],[3],[’abc’,4]Yes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值