Parisgabriel:python Full Stack engineer (0 basics to Mastery) Tutorial 11th

Source: Internet
Author: User
Tags iterable shallow copy

Parisgabriel Thank you for your support your reading evaluation is my best update power I'll stick to it. The typography is getting better .                                



Stick to a daily subscription every day. Grey often thanks for being a dead powder also wide to

                          Python ai from beginner to proficient

List creation function:
List (object can be iterated)
There are 5 types of sequences:
Star string
List lists
Tuple tuples
Bytes, behind the talk.
ByteArray, behind the talk.
Sequence operators:
In, not in
>
>=
<
<=
+
*

The index of the list can be incremented, deleted, modified, and the list of element values
Slice assignment:
The step size is not equal to 1 o'clock the slice assignment, the right side of the assignment operator, the element provided by an iterative object
must equal the number of slices cut out.
Del statement:
Element used to delete a list
 del list [index]
 del list [slice]
Sequence-related functions commonly used in Python3:
  len (x) returns the length of the sequence
  Max (x) returns the largest element
  min (x) returns the smallest element of the sequence
  sum (x) returns all the primitives in the sequence and
  Any (x) Truth test if there is a value in the list that returns true
  All (x) Truth test if all values in the list are true return True

method of the list:
Help (list)

the parameters within [] can be omitted
     Method                        meaning
L.index(v [, begin[, end]]) returns the index subscript of the corresponding element,begin:To start the index,End: To end the index and trigger a VALUEERROR error when value does not exist
L.insert(index, obj) inserts an element into the specified position in the listIndex: Indexobj: the element to insert
L.count(x) Returns the number of elements in the listx: element
L.remove(x) Remove the first value from the list that appears in the listx:Element (for example, the list has 2 3 deletions, the first one in turn)
l.copy() Copy this list (only one layer is copied and deep objects are not copied)
L.append(x) append a single element to the list to append an iterative object element
L.extend(list) Append another list to the list
L.clear() empty list, equivalent tol[:] = []
L.sort(Reverse=false) To sort the elements in the list, in the default order by valueSmall to large order of arrangement
L.reverse() List ofreversalThat is used to change the original list. Sequencing
L.pop([index]) Delete the element corresponding to the index, if not indexed, by default delete the last element, while returning the reference relationship of the deleted element (the equivalent of removing the element is not returned, you can use the variable to accept)
text parsing methods for strings:
S.split(Sep=none) splits a string using Sep as a delimiter, returns a delimited list of strings, separated by white space characters when no arguments are given
S.join(iterable) returns a string in the middle of an S-delimited combination with a string in an iterator object S=esp the character iterable to insert the split link: an object that can be iterated

Latent copy and deep copy:

latent copy shallow copy:
Help (List.copy)
Copy refers to copying only one layer of variables during the copy process, and does not copy the copy of the object that is bound by the deep variable .
For example:

>>> L = [3.1,3.2]>>> L1 = [1, 2, L]>>> L2 = l1.copy ()>>>
   
     l1[1, 2, [3.1, 3.2
    ]]>>>
     l2[1, 2, [3.1, 3.2
    ]]>>> l2[2][0] = 3.14 >>>
     l1[1, 2, [3.14, 3.2
    ]]>>>
     l2[1, 2, [3.14, 3.2]]
   

deeply copy deep copy:
Import Copy # importing copy module
For example:
L = [3.1, 3.2]
L1 = [1, 2, L]
L2 = l1.deep copy ()
L2[2][0] = 3.14
Print (L1) #[1, 2, [3.1, 3.2]]
Print (L2) #1, 2, [3.14, 3.2]]

A deep copy is usually copied only on Mutable objects , and immutable objects are usually not duplicated (deep copies here need to be deepcopy in the import copy)

Summary:
L1 = [1, 2, [3.1, 3.2]]

Here is just a purely call relationship can be viewed via memory address

L2 = L1 # does not copy bind an object at the same time
L3 = L1.copy () # latent copy equals L3 = l1[:]
Import Copy
L4 = Copy.deepcopy (L1) #深拷贝

List-derived lists comprehension:
List derivation is an expression that generates a list with an iterative object
Grammar:
[Expression for variable in iteration object]
Or
[Expression for variable in iteration object if truth expression]

Description
The IF clause of the for in expression can be omitted, and all generated objects will be evaluated when omitted
For example: Generate a list of 1~9 squares
l= [x * * 2 for X in range (1,10)]
List push-to-nested nesting:
Grammar:
l=[-expression
For variable 1 in iterator object 1 if Truth expression 1
For variable 2 in Iteration object 2 if truth expression 2]

OK, today's practice is much more, but it's easy to try to understand every question. He doesn't know how to check the above parameters.

If you want to get started it must write more code to write more and you will be more proficient, more skilled experience, try different ideas to analyze

When do you want to throw up?

Practice:

1.

A list is known:
L = [3, 5]

1) Change the original list to the following operations such as indexing and slicing:
L = [1, 2, 3, 4, 5, 6]
2) Reverse the list, delete the last element and print the list
...
Print (L) # [6, 5, 4, 3, 2]

Answer:

L = [3, 5= Range (1, 7) l[::-1] = range (1, 7)#  l[:] = [Range (6, 0,-1)]del l[-1 ]print(L)

2.
1. Write the program, let the user enter some integers, when input-1 o'clock end input, save these numbers in the list L
1) How many numbers does the print user enter?
2) What is the maximum number of printed numbers you have entered?
3) What is the minimum number to print the number you have entered?
4) What is the average of the numbers you have entered?

Answer:

L = [] whiletrue:a= Int (Input ("Please input at would integer (input '-1 ' over):"))    ifA <0: BreakL.append (a) l.sort ()Print("You input line number", Len (L))Print("TOP1:", l[-1])Print("Lower1:", l[0])Print("Average number:", SUM (L)/Len (L))

3.

1. It is known that there is a string
s = "100, 200, 300, 500, 800"
The list to convert to integers is stored in the L list

Answer:

" 100,200,300,500,800 "  = S.split (",")print(L)

2. Generate the first 40 Gustav (Fibonacci)
1 1 2 3 5 8 13 .....
Require that these numbers be saved in the list
Print these numbers

Answer:

 L = [1, 1]a  = 1b  = 1while   true:a  += b b  += a L  += [A, b]  if len (L) > 38:  break  print  (L) 
= [1, 1= 1== 0 while I <:    = a + b    = A- b    L.append (b)     + = 1print(L)

4.

l= [1, 3, 2, 1, 6, 4, 2, ... 98, 82]
Save duplicate numbers appearing in the list in another list L2
Requirements: Repeated occurrences of the number in the L2 only one copy (deduplication)

Answer:

L = [] whiletrue:a= Int (Input ("Please input at would integer (input '-1 ' over):"))    ifA <0: BreakL.append (a) L2= [] forXinchL:ifX not inchL2:L2.append (x)Print(L)Print(L2)

5.
Enter multiple lines of text into the list,
One row after each input carriage
Enter multiple lines at any time when entering enter (that is, entering empty lines to end input)
1) Output content on the screen according to the original input
2) Print out how many lines of text you have entered
3) Print out the number of characters you have entered

Answer

L =[]i=0 whiletrue:a= Input ("Please input at would string direct Enter over:")    ifA = ="":         BreakI+=Len (a) l.append (a) forXinchL:Print(x)Print("number of rows you have entered:", Len (L))Print("characters you have entered:"I

6.
1 A string "Hello"
Please use this string to generate:
' h e l l o ' and ' H-e-l-l-o '
2. Write a program that lets the user enter a number of positive integers, and when input is less than 0, end the input,
1) Print the largest of these numbers
2) Print the second largest number of these numbers
3) Delete the smallest number
4) Print the remaining number and

Answer:

" Heool "  "". Join (s)print"-" . Join (s) Print L

7.
To generate a list of 1~100 inside odd numbers using a list deduction
The result is: [1, 3, 5, 7, ..... 99]

Answer:

 for  in range (1, 2)]print(L)
 for inch if x% 2 = 0]print(L)

8.
Generate a list of squares with a value of 1 ~ 9, minus all the odd squares
Answer:

 for  in range (2, 2)]print(L)
 for inch if x% 2 = = 0]print(L)

Python ai from beginner to proficient

Come on!

Parisgabriel:python Full Stack engineer (0 basics to Mastery) Tutorial 11th

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.