Sequence / List

1. The sequence for storing values ​​of a plurality of contiguous memory space, according to a certain order

1) The index

1 to n-1

-n to -1

 

2) Slice

sname[start:end:step]

print (nba [2: 4]) # 3-5 to obtain the first element

print (nba [2: 4: 5] # of 3/5/6 element obtained

 

3) sequences are added

a = [21,33,44]

b=[1,2,4]

print (whether b)

 

4) Multiplication

print(b*3]

Output: [1,2,4,1,2,4,1,2,4]

 

emptylist = [none]*5

print(emptylist)

[none,none,none,none,none]

5) checks whether an element is a member of the sequence

value in sequence

print("1" in b)

Showing results True

print("1" not in b)

Showing results False

 

6) Calculate the length of the sequence, a maximum value, a minimum value

print ( "sequence length:", len (b))

print ( "Maxima:", max (b))

print ( "minimum sequence:", min (b))

 

list () is converted into a sequence listing

str () sequence into a string

sum () sums

sorted () to sort the elements

the reversed () element in reverse sequence

emumetate () while the output element and index values

 

2. [within] a list, in order, divided

     Content, the content of any real numbers, integers, strings, lists, and the like can be placed in the tuple list. List, may be different types of elements, there is no relationship between them.

 

listname =[element1 , element2 , element2 , element3,...]

emptylist = [] # Empty list

Create a list of values, list (range (10,20,2)) -> [10,12,14,16,18]

Delete List del #del emptylist

 

3. traverse the list

for item in listname:

  Output item #

 

for index,item in enumerate(listname):

  Output index and item #

 

4. Add the elements

listname.append (obj) #append element ratio command to add a "+" is more efficient, as this

listname.extend (seq) # a list of all elements added to the list to another

 

Modify elements direct assignment verse [-1] = "line on the blue sky egret"

Removing elements del verse [-1] # deleted according to element index

      vrese.remove ( "Bull") # Remove element values ​​according to

      value = "Bull"

      if verse.cont (value)> 0: # ensure that the elements present in the list

        verse.remove(value)

 

 

5. statistical and computing

  listname.count (obj) #obj appear in the list cis

  listname.index (obj) #obj index values ​​in the list

  sum (listname [, start]) # summation, start statistical results indicate the start index.

 

6. Sort

sort()

  listname.sort(key=None,reverse=False)

  listname: To sort the list represents

  key: Comparison of key, key = str.lower, showing a case-insensitive

  reverse: True descending, False ascending

 

 

sorted () # built-in functions will not change the original sequence

 

List comprehensions:

  1. import dandom

    randomnumber =[random.randint(10,100) for i in range(10)]

  2.

    price=[1,2,3]

    sale=[int(x*0.5) for x in price]

 

  3. sele= [x for x in price if x>4]

 

 

6. The two-dimensional list

 

  arr=[ ]

  for i in range(4):

    arr.append([])

    for j in range(5):

      arr[i].append(j)

 

List comprehensions

  arr =[[j for j in range(5)] for i in range(4)]

 

 

        

 

Guess you like

Origin www.cnblogs.com/lelin/p/11567046.html