Python 3 Learning Notes (3) - - Data Operations, Collection Operations and Document Operations

Keywords: Python encoding network

Data operation

Arithmetic operations:

Assignment operation:


Identity arithmetic:

Membership operations:

Logical operations:

Bit operation:

Operator priority:

II. Collective operations

A set is an unordered, non-repetitive data combination whose main functions are as follows:

  • De-duplicate, automatically deduplicate a list when it becomes a collection
  • Relational testing, testing the relationship between the intersection, difference set and union set before two sets of data
 1 print("-----------Relational testing-------------")
 2 list_A = [1,3,5,7,7,8,9]
 3 list_A = set(list_A) #Converting lists into collections
 4 list_B = set([0,2,4,5,6,8,9])
 5 print(list_A,type(list_A))  #The assembly will automatically weigh down
 7 print(list_A,list_B)
 9 #intersection
10 print(list_A.intersection(list_B))
11 print(list_A & list_B)
13 #Union
14 print(list_B.union(list_A))
15 print(list_B | list_A)
17 #Difference set
18 print(list_A.difference(list_B))  #stay list_A Yes, but list_B No.
19 print(list_A - list_B)
21 #subset
22 print(list_A.issubset(list_B)) #judge list_B Is it list_A Subsets
23 print(list_A.issuperset(list_B))  #judge list_A Is it list_B The parent set of
25 #Symmetric difference set
26 print(list_A.symmetric_difference(list_B)) #Take neither of the two sets.
27 print(list_B ^ list_A)
29 list_C = set([22,33,44])
30 print(list_B.isdisjoint(list_C)) #Determine whether two sets do not intersect, and if they do not intersect, return to True
 1 print("---------Collection operation-----------")
 2 list_A.add(444)  #add value
 3 list_B.add("hahaha")   #Adding strings
 4 print(list_B,list_A)
 5 list_C.update(([66,77,88]))  #Batch addition
 6 print(list_C)
 8 list_A.remove(444) #Delete the specified item
 9 list_A.pop()  #Random deletion list_A One item
10 list_A.discard(999)  #use discard Deleting a non-existent item will not cause an error. remove Deleting items that do not exist will cause an error
11 print(list_A)
13 3 in list_A  #Test 3 is list_A Membership. Lists, tuples, dictionaries, collections are all such operations

III. File Operation

Reading and Writing of Documents:

 1 f = open("Jigoku Shoujo Mioyosuka","r",encoding="UTF-8")   #r representative read Represents read, w representative write Represents write (new, overwritten) a representative append Representation of additions
 2 data =
 3 data2 =
 5 print(data)
 6 print("--------------",data2)   #here data2 The print is empty because of the first print. data The time has come to the end. data2 Printing starts at the end so there is no data.
 8 f.close()  #Close files
11 f1 = open("Hell Girl 1","w",encoding="UTF-8")   #write Is to create a new file, if the open file name does not exist, then create a new one, if it already exists, then overwrite it.
12 f1.write("Because to eliminate resentment,\n So exile to hell")
13 print(f1)
15 f1.close()
18 f2 = open("Jigoku Shoujo Mioyosuka","a",encoding="UTF-8")   #append It can only be added. read
19 f2.write("Because to eliminate resentment,\n So exile to hell")
20 print(f2)
22 f2.close()

Common operations of files:

 1 #f = open("Jigoku Shoujo Mioyosuka","r",encoding="UTF-8") #File handle
 2 #print(f.readline())    #readline is read by line
 3 #print(f.readline())
 6 #Read the first five lines of data
 7 #for i in range(5):
 8 #    print(f.readline())
11 #print(f.readlines()) #Converting files to a list
14 f = open("Jigoku Shoujo Mioyosuka","r",encoding="UTF-8") #File handle
15 print(f.tell())  #Find the current pointer location, which is the character location.
16 print( #Read 5 characters, usually not because you don't know if the end statement is complete.
17  #Return the pointer to its starting position
18 print(f.isatty())  #Determine whether it is a terminal device
19 print(f.seekable())  #Determine whether the pointer can return to its original position
20 print(f.readable())   #Determine whether a document is readable
21 print(f.writable())   #Determine whether a document is writable
23 print(f.flush())   #force refresh(In some cases where timeliness is strong, such as withdrawal procedures)
25 #print(f.truncate(10)) It is truncation. If you do not write the equivalent of empty, the input number represents truncation from the beginning of the 10th character. truncate Applicable only to _____________"append"

Document modification:

 1 f = open("Jigoku Shoujo Mioyosuka","r+",encoding="UTF-8")   #r+For reading and writing, read first and then write, commonly used
 3 print(f.readline())
 4 print(f.readline())
 5 print(f.readline())
 6 print(f.tell())
 7 f.write("------------------------------")
 8 print(f.readline())
11 f = open("Hell Girl 1","w+",encoding="UTF-8")    #w+To write and read, first write and then read, first create a file and then read, not often used.
13 f.write("----------------------------1\n")
14 f.write("----------------------------1\n")
15 f.write("----------------------------1\n")
16 print(f.tell())
18 print(f.readline())
19 f.write("I am Irlo.")    #Writing here is also added to the last line
20 f.close()
22 f = open("Hell Girl 1","a+",encoding="UTF-8")    #a+Not often used for additional reading
23 '''
25 #f = open("Jigoku Shoujo Mioyosuka","rb")    #Binary file reading, network transmission can only use binary mode
26 f = open("Hell Girl 2","wb")   #Binary File Writing
27 f.write("Hello Irlo\n".encode())

Example 1. Cyclic printing until line 10 stops, and continue printing after modifying line 10 data

Old methods (not recommended):

1 f = open("Jigoku Shoujo Mioyosuka","r",encoding="UTF-8") #File handle
4 for index,line in enumerate(f.readlines()):  #f.readlines It needs to be converted to a list first. It is inefficient and has limitations. It can only be used in the case of small files.
5     if index == 9:
6         print("-------------------")
7         continue
8     print(line.strip())   #strip It removes spaces and changes lines.

Method of not occupying memory:

 1 f = open("Jigoku Shoujo Mioyosuka","r",encoding="UTF-8") #File handle
 3 count = 0
 4 for line in f:          #Iterator, where only one row of data is stored in memory
 5     if count == 9:
 6         print("-----------------")        #"-------"Instead of the data in line 10
 7         count +=1
 8         continue
 9     print(line)
10     count +=1

Example 2. Progress Bar

1 import sys,time
3 for i in range(15):
4     sys.stdout.write("#")   #stdout It's standard output, usually output to the monitor.
5     sys.stdout.flush()
6     time.sleep(0.1)

Annex. Hell Girl

 1 resentment:
 2 In the eighth episode of Hell Girl (Season 1), Chaida failed to log in to Hell Communications, which shows that the entry of Hell Communications is conditional. Most successful entries from different episodes share a similar common ground, which is called "resentment" in the story. The degree of resentment is often one of the key factors affecting the development of the story, but the definition of "resentment" in animation is not clear. The following is the classification.
 3. resentment caused by framing:
 4. Some people have been framed, for example, the cursed man Hua Li guard in the third episode of Hell Girl (Season 1) killed his client's friend Dafu Iwata, but put the responsibility of killing him on Dafu Iwata. [14] So Dafu Iwata resented and wanted to exile the cursed man.
 5. resentment caused by injuries to important others:
 In the fourth episode of Hell Girls (Season 1), the client's beloved dog was indirectly killed by doctors after being delayed in diagnosis and treatment. In the next episode, it was because the client's father was killed that he was exiled to hell. In animation, the client's heartache sometimes shows his deep feelings with important personnel to illustrate its importance.
 7 Causing resentment for satisfying one's personal desires:
 For example, in the seventh episode of the first season, the client, the Red Flower (and the Cursed), had to exile his foster mother to hell in order to get his inheritance. Twenty-four episodes in two cages even make it clear that exile is to satisfy one's desires.
 9 resentment caused by "betrayal":
The hatred of the cursed also affects the development of the story. In the ninth episode of Hell Girl (Season 1), the cursed person framed the client's sister and the client because the client's sister "betrayed" herself, causing the client's "resentment". But betrayal is not always easy to understand. It seems to be said in the story that one side does not accept the other side's feelings, leading to resentment. See also the cursed people in the second episode of the first season.
11 Causing resentment for the benefit of others:
In order to prevent others from losing their rights and interests, such as property and life, and send the cursed person to hell, this resentment is entirely derived from the client's personal conscience and empathy. For example, in the sixteenth episode of the second cage, in order to prevent others from being deceived and framed to death by the cursed person, the client unlocks the red line before dying and sends the cursed person to hell, but then dies and the candle extinguishes.
13 resentment for unknown reasons:
14 Sometimes stories do not necessarily reveal something that is considered to be hateful. For example, in the 23rd episode of Hell Girl Season 1, the cursed person (a female nurse) shows only the good side. But from the exile of the cursed person to hell and the words "we will eliminate hatred for you" in Hell Communications, we can see that in the story, "hatred" is eliminated. Now. Although this kind of "resentment" exists according to the story, it lacks powerful reasons to understand why it exists.
15 resentment that may differ from general understanding:
The general concept of hatred is hatred, but it is not always accepted in stories, sometimes even the opposite. For example, in the twelfth episode of the first season, the principal and the cursed forgive and sympathize with each other. As a general understanding, they should have no resentment, but the principal exiles each other to hell. In the twentieth episode of the second cage, the cursed person actively helps the principal unlock the red line in order to accompany each other into hell.
17 resented for meddling:
When a person is worried that the other party wants to help him solve the problem, but the other side asks not to be meddlesome, but the other side insists on helping, which leads to resentment from the other side. For example, in the seventh concentration of Sanding, the cursed person is not accustomed to the irresponsible behavior of the client's mother. As a classmate, he persuaded the client to speak out and intended to tell the teacher that the client asked the other party not to meddle, but the cursed person still insisted on meddling. However, in order to protect this seemingly unfortunate happiness, the client chose to exile the cursed person.
19 resentment for sealing up:
In order to protect their reputation or interests, they should resent those who hurt themselves, but sometimes they resent those who hurt themselves, even those who have been good to them. In the 14th episode of Sanding, the principal becomes a friend because of the intimate rumors that others maliciously exile him and the cursed concurrently, but the principal is only helped by the kindness of the other party. At first, the client wanted to banish the person who maliciously scattered the photos, but he did not know the name of the object. Later, it was found that the cursed person was arrested for committing a crime and had helped his cursed person by choosing exile in order not to let the rumors grow.
21 It should be noted that the resentment in the story is not necessarily just one of the above-mentioned meanings. Sometimes it may be a mixture of different kinds.

Posted by JJ123 on Thu, 06 Jun 2019 12:39:25 -0700