程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python interview questions_ Chapter (5)

編輯:Python

List of articles

      • 1. How to modify global variables inside a function
        • ① python What are global variables in
        • ② If a global variable appears in the scope of a function , What are the characteristics
        • ③ How to set a function scope ( In local scope ) Modify the value of the global variable ?
      • 2. Dictionary how to delete key and merge two dictionaries
        • ① Delete key
        • ② Merge two dictionaries
        • ③ In this paper, a brief introduction of object-oriented is given `__init__` and `__new__` The difference between ?
      • 4. sketch with Method to open the processing file to help us do ?
      • 5. list [1,2,3,4,5], Please use map() Output function [1,4,9,16,25], And use the list derivation to propose greater than 10 Number of numbers , Final output [16,25]
        • ① map Use
        • ② answer :
      • 6. Python To generate random integers , Random decimal ,0-1 Between decimals
        • ① random.randint(start,end)
        • ② random.random()
        • ③ random.rand()

1. How to modify global variables inside a function

① python What are global variables in

  1. Global variables are variables declared outside the function
  2. The variables declared in the function body are local variables .

② If a global variable appears in the scope of a function , What are the characteristics

  1. You can access global variables directly
  2. But it can't be assigned directly , When assigning values directly , The compiler will think it has recreated a local variable , Only the local variable and the global variable have the same name , Then the global variables will be overwritten .

③ How to set a function scope ( In local scope ) Modify the value of the global variable ?

  1. Need help global Keyword declaration
  2. global The declaration tells the compiler , This variable is a global variable , Then it will be used as a global variable when using and compiling .

2. Dictionary how to delete key and merge two dictionaries

① Delete key

Method 1: The first reaction is pop


As can be seen from the above example , Dictionary pop(key) You can really delete the key of the dictionary , But there are two requirements

  1. pop(key) Function has no default arguments , You must provide a parameter key .
  2. pop(key) Parameters key Must be a key present in the dictionary , Otherwise you will report an error

solution 2: The second reaction is remove, Not sure remove Can you , Try it .


The attempt failed , There is no... In the dictionary remove Method , And then use dir(dict) Look at some attribute methods of the dictionary : as follows

Notice that there is a popitem Method , This method and pop What's the difference ? Let's see what the official explanation says ?


Translate :
The general meaning is to remove certain key( Parameters passed in ), And return its corresponding value. If key non-existent , If default values are provided , Return the default value , If not provided , Report on KeyError


You can see it popitem() There is no parameter requirement , It removes a key value pair , And back to , But it is not clear which key value team to remove . If the dictionary is empty , Report on KeyError


The third reaction : del


del dct[key] You can also delete the key of the dictionary , stay key It also reports an error when it does not exist . The return value is None

② Merge two dictionaries

Method 1: There is a in my memory update Method to merge dictionaries , But the specific application is not very clear . Let's see what the document says ?


[E,]**F -> None. What is this ? Don't worry , Encounter this kind of thing , I also feel very upset , Why do other people write documents , We don't understand . Take your time first .

[E,] This kind of bracketed [] All of them indicate that this parameter is optional , Can provide , It is also possible not to provide , There are default parameters in the definition of this function .
**F Express It receives a variable number of key parameters as F.
If E Provides and has a keys() Method , And then go through it , Assign a value to D
If E Yes, but no keys() Method , And then it goes through k,v, Assign a value
Either way , It will be carried out for k in F: D[k] = F[k]

First of all, there are only E And E and F In the meantime :


Look again, only F The situation of :

Method 3: Constructors dict(d1,**d2)

Obvious , This approach has one limitation , That is, the second dictionary must be a string as a key .

Method 4: Dictionary derivation


Be careful : This dictionary derivation is double nested , Because you have to traverse two dictionaries first , Then each dictionary uses a derivation to derive the assignment .

Method 5: Element stitching , Splice through the list , Then convert to a dictionary

Method 6: Dictionary splitting , Through curly braces {}, It can directly form a new dictionary

newDict = {**d1,**d2,**d3,...}

③ In this paper, a brief introduction of object-oriented is given __init__ and __new__ The difference between ?

There are two main differences :
Functionally speaking :

  1. __init__ It is mainly used to initialize objects , It calls on the premise that __new__ When the current object is returned ,__init__ Will be called .
  2. __new__ When constructing objects , adopt __new__ To create an instance of the current object .

The timing and conditions of the call :

  1. __new__ Generally, there is no need to define , It is automatically created by the compiler , When creating objects , First call __new__.
  2. __init__ Generally, you need to define yourself , Used to specify your own personalized initialization , When creating objects , First call __new__, Normally this __new__ An instance of the current object will be returned , If an instance of the current object is returned, it will automatically call __init__ Method , If not __init__ Method will not be called

Parameter and return value :

  1. __new__() At least one cls Parameters , Represents the current class , This parameter is instantiated by Python The interpreter automatically recognizes . Generally, you need to return the current instance .
  2. __init__() At least one parameter self, This self In fact, that is __new__ The returned instance . It's usually back to None

Look at if __new__ The current instance is not returned , And that is __new__ This parameter cls What is it ?


You can see __new__() This one of them cls In fact, it's time Person class , But what we return above is not Person Instance object of , therefore __init__ There is no call at all .


You can see that if you use Person Class to instantiate an object ,__init__ It will be called . It's directly used here object.__new__(Person) Actually sum super().__new__(cls) It is equivalent. .

4. sketch with Method to open the processing file to help us do ?

Open the file without with Writing :

Be careful : here a.txt An exception is reported if it cannot exist . So we added try catch, And because you have to close the file stream anyway , Let's add another finally.


As you can see from the example above with Statement does something .

  • 1) Throw an exception .
  • 2) Close file stream .

with The principle of statement implementation :

with The statement is implemented by Context manager Realized , Not all methods can be implemented with sentence , Only those that implement the context management protocol can be used with sentence .

Context Manager Protocol

  1. __enter__(self) Method ,with When the statement starts running , Will be called on the context manager __enter__ Method , This function usually returns an object , It's usually self , The return value of this function is assigned to the variable following as On the next variable .
  2. __with__ At the end of the statement , Called on the context management object __exit__(self,exc_type,exc_value,traceback) Method , In order to play finally The role of the clause . Let's see what these three parameters are exc_type,exc_val, exc_tb

Write a context manager , Then see how it works .


Essentially , If an exception is thrown from anywhere within the block , Will __exit__() Call the function of the object . As you can see , The type associated with the exception thrown , Values and stack traces are passed to this function . under these circumstances , You can see ZeroDivisionError Throw an exception . The person who implements the library can __exit__() Function to write cleanup resources , Close the file and other codes .

5. list [1,2,3,4,5], Please use map() Output function [1,4,9,16,25], And use the list derivation to propose greater than 10 Number of numbers , Final output [16,25]

① map Use

My first reaction map It's a mapping function , The front is an iteratable sequence , The following is the corresponding mapping method , It should look like this
map(iterable,somefunction)

Let's test it :

This is obviously wrong , Say that functions cannot be iterated . Is it because I remember it backwards , The previous is the mapping method , Then there are iteratable objects , I'll try again .


Sure enough , It's a mistake . The previous is the mapping method , Then there is the iteratable object , So when I was doing this problem , Just extend a thought , Why can't I have an iteratable object in front of me , The following is the mapping method ?

The answer is simple , Because there may be more than one iteratible object , But mapping methods can only have 1 individual , Take a look at the following example of mapping with two iteratable objects :


What if the number of iteratible objects is different , As long as one iteration ends , The mapping ends .

And then when we get here , Actually, I'm still curious , If the number of iteratible objects , When it is greater than the number of mapped parameters , What's going to happen ?

You can see that the report is wrong , Empathy , If there are too few parameters , It's also a mistake .

② answer :

So I know map Usage of , The answer above is easy to solve , It can be solved directly by finding another layer of conditional list derivation .

6. Python To generate random integers , Random decimal ,0-1 Between decimals

① random.randint(start,end)

The first thing I think of is this , Generate random integers . Right or wrong , Let's go and have a look at , Under test .


Okay , Now let's take a look at the description of this function :


Translate for you :
Is to return to a [a,b] An integer between , contain a and b. If you verify , contain a and b Well

② random.random()

I have the impression that there is such a function to generate decimals , Is there , We still need to verify .

Sure enough , This function returns [0,1) A floating-point number of , Note that this category does not include 1, But it includes 0.

③ random.rand()

I remember this function , But I don't remember very clearly , Go and verify :

The fact proved that , There is no , What exactly is the function ? We use dir(random) look down


There are many functions in it , Let's see which one is more like what we're looking for . Let's see randrange() What is it ?


Translate, translate :
Get a random integer , from range(start,stop[,step]) Within the scope of , This solves randint() It contains the question of the last point . Sometimes , It may not be necessary for the last point to be randomly assigned to , You can use this method .


Although this is also a random number , But it's obviously not the way we're looking for to find random decimals , Let's move on to the next .

sample() Does it look similar? ? I really have no impression here , Just look at the documentation .


ah , So long , It's like giving up , But now that I have met , Just study what this thing is ?
In translation .......
The simple explanation is from population Select from this sequence or set k A unique random element .

Returns a new list containing from population Elements selected in , And maintain population The elements in the do not change .
The list of results is arranged in order , So all the sub slices are valid random samples .

This allows the winner of the raffle ( This sample ) It can be partitioned , Divided into first prize and second prize ( Child Columns )

The data in the sample set does not have to be hashable or unique , If the sample contains duplicate elements , Then each of the repeating elements may be the choice of the sample . This uncertain .

To select a sample within an integer range , Please use range() As a parameter . From a Total sampling , This is particularly fast and space efficient .


After getting here , In fact, I don't quite understand the first prize and the second prize , I think the general meaning is that its subsequence can still be regarded as a complete random sequence .

Then go and see that uniform() function , I found this when I checked the data , This is how to generate random decimals in the range .

Look at what the official documents say ?

Obtain by rounding [a,b] perhaps [a,b) The random number , It can also be a random decimal .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved