2021-03-24

WARNING: This article may be obsolete
This post was published in 2022-03-24. Obviously, expired content is less useful to users if it has already pasted its expiration date.
This article is categorized as "Garbage" . It should NEVER be appeared in your search engine's results.


python List和Dictionary复习(补充没接触过或者已经遗忘的内容)

remove

a = [1, 2, 3, 3, 3, 3, 4]
a.remove(3)
print(a)

# [1, 2, 3, 3, 3, 4]

copy和deepcopy

https://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html
from copy import deepcopy

a = [1, 2, 3, 4, [2, 3, 4]]
b = a.copy()
c = deepcopy(a)
# 修改对象[2,3,4]变为[2,3,4,5]
a[-1].append(5)
print(a)  # [1, 2, 3, 4, [2, 3, 4, 5]]
print(b)  # [1, 2, 3, 4, [2, 3, 4, 5]]
print(c)  # [1, 2, 3, 4, [2, 3, 4]]
# 删除数组a中,对"数组对象[2,3,4,5]的引用"
a.pop()
print(a)  # [1, 2, 3, 4]
print(b)  # [1, 2, 3, 4, [2, 3, 4, 5]]
print(c)  # [1, 2, 3, 4, [2, 3, 4]]

运行结果:

[1, 2, 3, 4, [2, 3, 4, 5]]
[1, 2, 3, 4, [2, 3, 4, 5]]
[1, 2, 3, 4, [2, 3, 4]]
[1, 2, 3, 4]
[1, 2, 3, 4, [2, 3, 4, 5]]
[1, 2, 3, 4, [2, 3, 4]]

Dictionary的’in’用法

'in'不能用于检查value是否存在,只能检查key是否存在:

>>> dict={'key1':'val1','key2':'val2'}
>>> dict
{'key1': 'val1', 'key2': 'val2'}
>>> 'key1' in dict
True
>>> 'val1' in dict
False
>>> 'key2' in dict
True
>>> 'val2' in dict
False
>>> 'val2' in dict.values()
True
>>>

python List methods and functions补充还没用过的方法

Methods

🔗 [Python List/Array Methods] https://www.w3schools.com/python/python_ref_list.asp

Functions

注意,max()和min()可以使用的范围很大,不仅仅是数字类型的List,各种各样的List都可以使用,见:🔗 [Python 3 List Methods & Functions] https://www.python-ds.com/python-3-list-methods


OCR辅助查找工具

List def remove (self, __value: _T) Remove first occurrence of value. - > None Raises ValueError if the value is not present. ‘remove (self, __value)’ on docs.python.org … 1、 b=a:赋值引用,a和b都指向同一个对象。 a= {1: [1, 2, 31) b=a {1:」 [1, 2, 3] 2、 b= a.copy0:浅拷贝,a和b是一个独立的对象,但他们的子对象还是指向统一对象(是引用) a = (1:[1,2, 31) {1:L b = a.copy0 [1, 2, 3] {1:M b = copy.deepcopy(a):深度拷贝,a和b完全拷贝了父对象及其子对象,两者是完全独立的。 a = {1:[1, 2, 31) {1: b = copy.deepcopy(a) [1, 2, 3] {1:M [1, 2, 31 Python has a set of built-in methods that you can use on lists/arrays. Method Description append(). Adds an element at the end of the list clear(). Removes all the elements from the list copy. (.). Returns a copy of the list count(). Returns the number of elements with the specified value extend(.). index(). Add the elements of a list (or any iterable), to the end of the current list Returns the index of the first element with the specified value insert(). Adds an element at the specified position 90 e pop(.). Removes the element at the specified position remove(). Removes the first item with the specified value reverse(.). Reverses the order of the list sort (). Sorts the list


 Last Modified in 2023-02-07 

Leave a Comment Anonymous comment is allowed / 允许匿名评论