python第二次作业

1.list和tuple的区别

首先tuple元组内的元素无法被赋值,list列表内元素可以被赋值

然后tuple可使用的方法仅有count计数和index索引两种,而list可使用的方法有10种

2.学到的序列仅有string字符串,bytes字节,tuple元组,list列表

3.list中的方法使用

共11种方法

 |  append(self, object, /)
 |      Append object to the end of the list.(将对象附加到列表的末尾)

 |  clear(self, /)
 |      Remove all items from list.(从列表中删除所有项目)

 |  copy(self, /)
 |      Return a shallow copy of the list.(返回列表的浅拷贝副本)

 |  count(self, value, /)
 |      Return number of occurrences of value.(返回值的出现次数)

 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.(添加可迭代的元素来扩展列表)

 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.(返回第一个索引到的值)
 |      
 |      Raises ValueError if the value is not present.(如果值不存在,则引发ValueError)

 |  insert(self, index, object, /)
 |      Insert object before index.(在索引之前插入对象)

 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).(删除并返回索引处的项目(默认为最后一个))
 |      
 |      Raises IndexError if list is empty or index is out of range.(如果列表为空或索引超出范围,则引发IndexError)

 |  remove(self, value, /)
 |      Remove first occurrence of value.(删除第一次出现的值)
 |      
 |      Raises ValueError if the value is not present.

 |  reverse(self, /)
 |      Reverse *IN PLACE*.(位反转)

 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.(按升序对列表排序,然后返回空值)

4.将元组和列表转化成字典

 

 5.dict中的方法使用

共10种

 |  clear(...)
 |      D.clear() -> None.  Remove all items from D.

 |  copy(...)
 |      D.copy() -> a shallow copy of D

 |  get(self, key, default=None, /)
 |      Return the value for key if key is in the dictionary, else default.(如果键在字典中,则返回键的值,否则为默认值)

 |  items(...)
 |      D.items() -> a set-like object providing a view on D's items(一个类似集合的对象,提供D项的视图)

 |  keys(...)
 |      D.keys() -> a set-like object providing a view on D's keys(提供D键视图的类似集合的对象)

 |  pop(...)
 |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.(删除指定的键并返回相应的值。)
 |      
 |      If key is not found, default is returned if given, otherwise KeyError is raised(如果找不到键,则返回默认值(如果给定),否则会引发KeyError)

 |  popitem(self, /)
 |      Remove and return a (key, value) pair as a 2-tuple.(移除(键、值)对并将其作为2元组返回)
 |      
 |      Pairs are returned in LIFO (last-in, first-out) order.(对按后进先出的顺序返回)
 |      Raises KeyError if the dict is empty.(如果dict为空,则引发KeyError)

 |  setdefault(self, key, default=None, /)(增加元素)
 |      Insert key with a value of default if key is not in the dictionary.(如果关键字不在字典中,请插入默认值的关键字)
 |      
 |      Return the value for key if key is in the dictionary, else default.(如果键在字典中,则返回键的值,否则为默认值)

 |  update(...)(更新,如果被更新的字典中己包含对应的键值对,那么原 value 会被覆盖;如果被更新的字典中不包含对应的键值对,则该键值对被添加进去)
 |      D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
 |      If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
 |      If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
 |      In either case, this is followed by: for k in F:  D[k] = F[k]

 |  values(...)
 |      D.values() -> an object providing a view on D's values(提供D值视图的对象)

6.list或dict浅拷贝画图解释

b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象(是引用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值