python in the list of common operations

List describes

Think about it:

String in front of a bunch of learning can be used to store information, then think about how to store the names of all the students in our class do?

Define 100 variables, each store a student's name can you? There is a better way?

A: List

Format list: A type of variable as a list

namesList = [ 'xiaoWang', 'xiaoZhang', 'xiaoHua']

C than an array of powerful local language that elements in the list can be different types of

testList = [1,'a']

Print List:

demo:

namesList = ['xiaoWang', 'xiaoZhang', 'xiaoHua']
print(namesList[0])
print(namesList[1])
print(namesList[2])

result:

xiaowang 
Xiaozhang
xiaohua

 Loop through the list of

1. Use a for loop

For each data output list of more efficient, can be accomplished using a loop

demo: 

namesList = ['xiaoWang', 'xiaoZhang', 'xiaoHua']
for name in namesList:
print(name)

result:

xiaowang 
Xiaozhang
xiaohua

2. Use a while loop

For each data output list of more efficient, can be accomplished using a loop

demo:

namesList = ['xiaoWang', 'xiaoZhang', 'xiaoHua']
length = len(namesList)
i = 0
while i < length:
print(namesList[i])
i += 1

result:

xiaowang 
Xiaozhang
xiaohua

List of related operations

Data is stored in the list can be modified, such as "add", "delete", "change", "check"

<1> additive element ( "increase" append, extend, insert)

append: you can add elements to the list (tail) by append

demo:

 

 

 result:

 

 

extend: one by one can add another set of the elements extend through to the list

demo: 

 

 

 result:

 

 

 append and extend the difference?

append demo:               extend demo:

 

 

 

 Results: The results:

 

 

 

 insert: insert (index, object) prior to the specified object access element location index

demo:

 

 

 result:

 

 

 <2> modified element ( "change")

 Modify elements of time, to determine which element is to be modified by the subscript, and then to make changes

demo:

 

 

 result:

 

 

 <3> Find elements (check in, not in, index, count)

The so-called lookup, is to look at the specified element exists

in,not in

Python find common method of:

in (there is): If there is then the result is True, otherwise False

not in (does not exist): If there is then the result is True, or False

demo:

 

 

result:

 

 

Description:

The method will be used in long, then the same is not in use, but not in judgment does not exist

not in

demo:

 

 

result:

 

 

index,count

index and count with the use of the same string! ! !

demo:

 

 

result:

 

 

<4> Remove elements ( "delete" del, pop, remove)

In real life analogy, if a student transfer classes, then it should be left to the transfer student's name removed, often used in the development of this function deleted

Common delete method list elements are:

  1. del: delete according to index
  2. pop: Removes the last element (also may be deleted as subscript)
  3. remove: remove as value elements

demo (the)

 

 

result:

 

 

demo:(pop)

 

 

result:

 

 

demo:(remove)

result:

 

 

<5> sorted (sort, reverse)

sort the list is rearranged in a specific order, the default is small to large, reverse = True parameters can be changed to reverse, descending

reverse method is to list Retrograde

demo:

 

 

result:

 

 

Nested list

Similar nested while loop, also supports nested list, a list is a list of the elements, then this is a nested list

schoolNames = [[ 'Peking', 'Tsinghua University'], [ 'Nankai', 'Tianjin University', 'Tianjin Normal'], [ 'Zhejiang University'], [ 'Hebei University', 'Hebei University of Science and Technology ']]

demo:

 

 

result:

 

Guess you like

Origin www.cnblogs.com/jiaxinzhu/p/11785082.html