1. 程式人生 > 實用技巧 >python爬蟲——大學排名

python爬蟲——大學排名

1、列表

  列表由一系列按特定順序排列的元素組成,元素可以是字母,數字或其他字元,且元素之間沒有任何關係

  在python中,用方括號([])來表示列表,並用逗號來分隔其中的元素

  bicycles=['trek','cannondale','redline']

  1.1、增加

    方法apend()

    方法insert()

  1.2、刪除

    del語句

    方法pop()--刪除後可繼續使用

    方法remove()

  1.3、排序

    方法sort()--永久性排序

    函式sorted()--臨時排序    

    方法reverse()--反轉排序(永久性,再次reverse()恢復)

2、元組

  元祖是一系列不可修改的元素

  不可變的列表被稱為元祖,元祖使用圓括號來標識

dimensions=(200,50)

  print(dimensions[0])

  print(dimensions[1])

3、字典

  字典是一系列鍵-值對。每個鍵都與一個值相關聯,與鍵相關聯的值可以輸數字,字串,列表乃至字典。

  字典用花括號{}中的一系列鍵-值對錶示

  alien_0={'color':'green','points':5}

  print(alien_0['color'])

  print(alien_0['points'])

  巢狀

  3.1字典列表

  alien_0={'color':'green','points':5}

  alien_1={'color':'yellow','points':10}

  alien_2={'color':'red','points':15}

  aliens=[alien_0,alien_1,alien_2]

  for alien in aliens:

    print(alien)

  3.2在字典中儲存列表

  pizza={

    'crust':'thick',

    'toppings':['mushrooms','extra cheese'],

    }

  3.3在字典中儲存字典

  users={

    'aeinstein':{

      'first':'albert',

      'last':'einstein',

      'location':'princeton',

      },

    'mcurie':{

      'first':'marie',

      'last':'curie',

      'location':'paris',

      },

    }

4、函式

  向函式傳遞資訊

  4.1實參和形參

  def greet_user(username):

    print("Hello,"+username.title()+"!")

  greet_user('jesse')

  變數username是一個形參——函式完成其工作所需的一項資訊

  值'jesse'是一個實參,實參是呼叫函式時傳遞給函式的資訊  

  4.2傳遞實參

  位置實參——要求實參的順序與形參的順序相同

  關鍵字實參——其中每個實參由變數名和值組成

  預設值——可給每個形參指定預設值,給形參指定預設值後,可在函式呼叫中省略相應的實參

  讓實參變成可選的——給形參middle_name指定一個預設值——空字串,並將其移到形參列表的末尾

  傳遞列表

def greet_users(names):
for name in names:
msg="Hello,"+name.title()+"!"
print(msg)

usernames=['hannah','ty','margot']
greet_users(usernames)

  傳遞任意數量的實參——形參名*toppings中的星號讓python建立一個名為toppings的空元組,並將收到的所有值都封裝到這個元組中。

def make_pizza(*toppings):
print(toppings)

make_pizza('pepperoni')
make_pizza('mushrooms','potato')

  使用任意數量的關鍵字實參——形參**user_info的兩個星號讓python建立一個名為user_info的空字典,並將收到的所有名稱-值對都封裝到這個字典中

def build_persion(first,last,**user_info):
profile={}
profile['first_name']=first
profile['last_name']=last
for key,value in user_info.items():
profile[key]=value
return profile

user_profile=build_persion('jimi','hendrix',location='lunden',field='physics')
print(user_profile)

  4.3返回值

  返回簡單值

  返回字典 

def build_persion(first_name,last_name,age=''):
persion={'first':first_name,'last':last_name}
if age:
persion['age']=age
return persion

musician=build_persion('jimi','hendrix',age=27)
print(musician)