Hard Disk Partitions and File Systems in Linux

1. List

========== Understanding Lists ===========

1. List: store grouped information, composed of a series of elements in a specific order [most lists are dynamic]
[list names are generally plural, such as letters, cars, names
format: listName=[' ', /" "] ]

2. The index starts from 0 instead of 1, -1, -2... The index represents the first and second elements from the bottom, respectively

========= Element Operations==============

1. Access list elements by index: ————————————–
{ cars=['Maserati', 'Cadillac', 'Rolls-Royce']
print(cars[0])
Maserati}
2. Modify, add and delete elements ——————————————
—Modify: listName[index value]=” ” //Reassignment
Change the value of range elements: listName[0:5]=[value, ~/value]
Change the table step size to modify the element: listName[::2]=[~] //The step size is 2
- add: listName.append("element name") // [ə'pɛnd], append an
empty list Add: listName.append("element name")
~~
[expend() //expand to sequence]
Insert: listName.insert(index value," ") //must specify index and value
listName[3:3]=[ ' ']
—Delete: del listName[index value]
.pop(index value) //Retrieve and delete, the last one by default //[pɑp], take out
remove("element name")//There are duplicates to delete the first one Value
. clear() //Empty the list
[all can be deleted according to the slice [ : ]
pop() remove() are all methods; del is a statement]

====== Organize the list (adjust the order of elements) ======

1. Temporary positive and negative order (function: sorted( ))
print( sorted(listName) )
print( sorted(listName, reverse=True) )
2. Permanent positive and negative order (method: sort( ))
listName.sort()
listName .sort(reverse=True)
//listName.sort(key=lambda a:a[-1]) //Sort by the last letter
3. Check the length of the list
len(listName)
[Printable when IndexError occurs length/index value -1 to find logical errors]

=================== Manipulate the list (do the same for each element) ===================
1. Traverse List
cars=['Volkswagen','Benz','QQ']
for car in cars:
print(car) [variables are singular, list names are plural, ":" indented]
/for car in range(0,len(cars )):
print(cars[car])
[Avoid indentation errors!

2. The function range(firstNum, finalNum) prints numbers
[Tips: print from the first value to the end of the value before the last index!

3. Create a list of values ​​(store a set of numbers), and convert the function list() into a list
{
list( range(1,11) )

}
{
Create a list of squared numbers from 1-10
squares=[]
for value in range(1,11):
squares.append(value*value)
print(squares) }
[ print(list('abc')) / /str to list
print(('a','b','c')) //tuple to list]

4. Statistics list
min() /max() /sum() // Find the minimum/maximum/sum value in the list

5. List comprehension (merge the for loop and the new element into one line, and automatically append the new element)
such as 1-10 square can be:
squares=[value*value for value in range(1,11)]
list name=[expression loop to provide values ​​to expressions]

6. List slicing (using part of the list)
syntax: listName[start index: end index]
[Same as range(), no end value is taken! ]
{
players=['Guan Yu','Zhang Fei','Zhao Yun',' Han Xin','Jiang Ziya']
print(players[2:4])[Zhao Yun Hanxin//Tips: The corresponding element is +1, the current 4]
print(players[:3])
print(players[2:])
print( players[-2:])
}
A. Traverse the slice
for newValue in listName[start index: end index]:
print(newValue)
B. Copy the list
- newListName=listName[ : ] //Assign the entire list slice to newListName
- newListName=listName.copy()
//listName2=listName1 will cause a shared reference

Replenish:

Repeat
listName*n //n means: number of prints
Detect the first position of an element
listName.index(elementName)

Count the number of occurrences of an element
print(listName.count(elementName))

======= Tuple ======

1. Features
Unordered Immutable
sequence Collection that
can contain arbitrary objects
Subscript access
Arbitrary nesting Immutable
in-situ changes
2. Define a tuple
tupleName=(element_0, element_1)
3. Access a tuple
print(tupleName[0 ])
4. Access a specific range element through slice
print(tupleName[:])
5. Unmodifiable, delete
6. Define a tuple with only one element
one_tuple=("one",) //Need to add a ","
7 .Loop through tuples
for t in range(0,len(tupleName)):
print(tupleName[t])
/for t in tupleName:
print(t)
8. Tuple sorting
newTupleName=sorted(tupleName)
//after sorting , will return a new list, the tuple itself will not change
9. List to
tuple tuple(listName)

========= Dictionary (dict) =========

1. Meaning A series of -value pairs in a
dynamic structure (unordered, variable), each key is associated with a value, you can use the key to access the corresponding value, and associate the information
[Python does not care about the key value The order in which pairs are added, only the relationship between keys and values ​​is concerned]

2. Format:
dict_name={
'key':'value',
~~
}
[value: can be: number, string, list, dictionary;
key values ​​cannot be the same, the latter will overwrite the former]

3. Access the dictionary value
dict_name['keyName']

4. Add key-value pair
dict_name['newKey']='newValue'

5. Define an empty dictionary
dict_name={}

6. Modify the value in the dictionary
dict_name['key']='newValue'
{//Judging the alien moving distance by modifying the value
alien_0={'x_position':0,'y_position':25,'speed':"medium "} #Set
the alien speed to 'fast'
alien_0['speed']='fast'
record the alien's initial x-axis position
print("Original x-position:" + str(alien_0["x_position"] ))
How far to move based on alien speed
if alien_0['x_position']=='slow':
x_increment=1
elif alien_0["speed"]=='medium':
x_increment=2
else:
x_increment=3
will new The position is assigned to the variable
alien_0['x_position']=alien_0['x_position']+x_increment
print(“New x_position is:”+str(alien_0['x_position'])) }

7. Delete key-value
del dict ['key']
//Key-value is permanently deleted

8. Traverse the dictionary
A. Traverse the key-value pair:
for key,value in dict_name.items():
//Use the method items() to put the variable key and value into a list, the variable name should be meaningful!
//The return is a tuple, access the key/value in the tuple:
for item in dict_name.items():
itemy[0/1]
B. Traverse keys:
a. Traverse all keys: for key in dict_name.keys( ) :
b. Traverse keys in order: for key in sorted(dict_name.keys( )):
c. Traverse a single key: key_list=list(dict_name.keys()) key_list[]
d. Get each key: key1,key2 ,key3,~=dict_name print(key1,key2,key3,~)

C. Traverse all values: for value in dict_name.values( ):
a. Deduplication for values: for value in set(dict_name.values()):zi
b. Find key by value: value_list=list(dict_name.values( ))
result=key_list[ value_list.index[ "~" ] ]

9. Clear the dictionary
dicName.clear()

10. Nested
A. List of dictionaries: (alien list)
dicName1={'key1':"'value1',~}
dicName2={'key1':"'value2',~}
listName=[dicName1, dicName2]
B. List dictionary: (describes each pizza ingredient)
dicName={
'listName':[' ',' ']
}
for l_N in dicName['listName']: //traverse the specified list in the dictionary
/for key,value in dicName.items(): //
for v in value: // Traverse the entire list dictionary
C. Dictionary dictionary: (store user information)
dicName1={
'dicName2':{'key','value'},
}

========= set =========

1. Features
Variable sequence, support in-situ changes
Can contain any type,
any nesting
, no duplicate objects are allowed (automatic deduplication)

2. Format
name_set{ 'set_0','set_1','set_2' }

3. Set deduplication//Using set features
– deduplicate lists
id_list = ['id_0','id_1','id_2','id_3']
id_set = set(id_list)
– deduplicate strings
string_set = set( "hello")

4. Create empty set
none_set = set { }

5. Add, delete, change, check
name_set = {'Xiaowang','Dawang'}
– Add:
name_set.add("Kevin") //Add
name_set.update(["Xiaoguang","Daguang ") ],["WG"]) //The sequence is added
print(name_set)
- delete:
name_set.remove("WG") //Remove non-existent elements and report an error
name_set.discard("WG") //Remove does not exist The element will not report an error
print(name_set)
- change:
for n_s in name_set:
if n_s == "Xiao Wang":
name_set.remove(n_s)
name_set.add("Kevin")
print(name_set)

6. Intersection and union
name_set1 = {'Zhang San','Li Si','Wang Wu'}
name_set2 = {'Sun Qi','Li Si'}
– Intersection:
name_set=name_set1 & name_set2
name_set=name_set1.intersection (name_set2)
- union:
name_set=name_set1 | name_set2
- difference:
name_set=name_set1 - name_set2 //remove intersection from name_set1
name_set=name_set2 - name_set1 //remove intersection from name_set2
- symmetric difference: // union- intersection
name_set=name_set1^name_set2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955297&siteId=291194637