Day 8 文件处理

一 集合 Sets

1.定义 Definition

Sets are used to store multiple items in a single variable.

Set items are unordered, unchangeable, and do not allow duplicate values.

Sets are written with curly brackets.

2.作用 Usage

Remove duplicate values from the array(quite useless)

3.Relational calculus关系运算

Intersection交

The intersection() method returns a set that contains the similarity between two or more sets.

Meaning: The returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.

Union并

The union() method returns a set that contains all items from the original set, and all items from the specified sets.

You can specify as many sets you want, separated by commas.

If an item is present in more than one set, the result will contain only one appearance of this item.

Difference差

The difference() method returns a set that contains the difference between two sets.

Meaning: The returned set contains items that exist only in the first set, and not in both sets.

Symmetric_difference交叉补集

The symmetric_difference() method returns a set that contains all items from both set, but not the items that are present in both sets.

Meaning: The returned set contains a mix of items that are not present in both sets.

4.Superset and Subset 子集

issuperset()

The issuperset() method returns True if all items in the specified set exists in the original set, otherwise it retuns False.

issubset()

The issubset() method returns True if all items in the set exists in the specified set, otherwise it retuns False.

5.Add set items 增

Add

To add one item to a set use the add() method.

Update

To add items from another set into the current set, use the update() method

The object in the update() method does not have be a set, it can be any iterable object (tuples, lists, dictionaries et,).

6.Remove set items 删

To remove an item in a set, use the remove(), or the discard() method.

Note: If the item to remove does not exist, discard() will NOT raise an error.

You can also use the pop(), method to remove an item, but this method will remove the last item. Remember that sets are unordered, so you will not know what item that gets removed.

The return value of the pop() method is the removed item.

7."改"操作无法实现

Update and Change is invalid

8.数据类型 Data type

Sets and lists can be converted to each other.

二 基本数据类型总结

数据类型一个/多个值有序/无序可变/不可变允许重复/不允许重复
数字型 一个 / 不可变 /
字符串 一个 / 不可变 /
列表 多个 有序 可变 允许
元组 多个 有序 不可变 允许
字典 多个 无序 可变 不允许
集合 多个 无序 可变 不允许

 

三 文件处理

1.Open

The key function for working with files in Python is the open() function.

The open() function takes two parameters; filename, and mode.

There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists

2.Read

To open the file, use the built-in open() function.

The open() function returns a file object, which has a read() method for reading the content of the file:

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

Read lines

By calling readline() two times, you can read the two first lines:

By looping through the lines of the file, you can read the whole file, line by line:

3.Close files

It is a good practice to always close the file when you are done with it.

Note: You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

四 补充了解

1.队列与堆栈 Queue and Stack

Stack

Stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle.

Queue

Queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle.

2.字典中setdefault用法(以及与update区别)

setdefault()

Returns the value of the specified key. If the key does not exist: insert the key, with the specified value

update()

Updates the dictionary with the specified key-value pairs

 

3.关于集合中的pop随机问题讨论

https://fishc.com.cn/thread-131913-1-1.html

posted @ 2020-12-24 21:36  fengshili0721  阅读(115)  评论(0编辑  收藏  举报