Python pen question 1-21

Source: Internet
Author: User
Tags python list

1, how to achieve the Python list to the weight and maintain the original order

A: Then create a list, loop the original list element and determine if the current element is not in the new list, then add

2, there are now two tuples ((' A '), (' B ')), ((' C '), (' d ')), use the anonymous function in Python to generate the list [{' A ': ' B '},{' C ': ' d '}]

For:

#答案一test = Lambda T1,t2: [{i:j} for I,j in Zip (t1,t2)]print (Test (T1,T2)) #答案二print (list (map (lambda t:{t[0]:t[1]},zip (T1, T2))) #还可以这样写print ([{i:j} for I,j in Zip (t1,t2)]))

3, please give two points to find the Python sample code

For:

  

def recursion_search (Data_source, find_n):
MID = Len (data_source)//2
If Len (data_source) >= 1:
If Find_n > Data_source[mid]:
Recursion_search (Data_source[mid + 1:], find_n)
Elif Find_n < Data_source[mid]:
Recursion_search (Data_source[:mid], find_n)
Else
Print (Data_source[mid])
Else
Print ("Not Found!")

4. The main difference between% and. format in Python string formatting

A: If you pass a number in%, you must specify the type, and format does not

5, *args,**kwargs Under what circumstances will be used, please example **kwargs application

A: *args,**kwargs is generally used for functions to pass multiple values,

**kwargs Example def x (**kwargs):

Return Kwargs

X (a=1)

6, x = ' foo '

y = 2

Print (X+y)

A.foo b.foo foo c.foo 2 D. An exception throw

7.

Kvps = {' 1 ': 1, ' 2 ': 2}
Thecopy = Kvps
kvps[' 1 '] = 5
sum = kvps[' 1 '] + thecopy[' 1 ']
Print (sum)

A.1 b.2 d.10

8, Sys.path.append ('/root/mods ')

A. Changing the python boot path b. Change Python's current working directory c. Add a new Python module search path d. Remove all folders from Mods

9.

Country_counter = {}

def addone (country):
If country in Country_counter:
Country_counter[country] + = 1
Else
Country_counter[country] = 1
AddOne (' China ')
AddOne (' Japan ')
AddOne (' China ')
a.0 B.1 c.2 D.3 e.4

10.

Names1 = [' Amir ', ' Barry ', ' chales ', ' Dao ']
Names2 = names1 #[' Alice ', ' Barry ', ' chales ', ' Dao ']
Names3 = names1[:]
Names2[0] = ' Alice '
NAMES3[1] = ' bob ' #[' Alice ', ' Bob ', ' chales ', ' Dao ']
sum = 0
For LS in (NAMES1,NAMES2,NAMES3):
If ls[0] = = ' Alice ':
sum + = 1
If ls[1] = = ' Bob ':
Sum + = 10
Print (sum)

b.12 c.21 d.22 e.33

11.

D = Lambda p:p*2
t = Lambda p:p*3
X= 2
x = d (x)
x = t (x)
x =d (x)
Print (x)
Answer: x = 24

12.

x = True
y = False
z = False
If x or Y and Z:
Print (' Yes ')
Else
Print (' No ')
Answer: Yes (and priority greater than or)

13.

How to implement Touple and list conversions in Python

Answer: touple (list)->touple list (touple)->list

14.

Please write a code implementation to delete the repeating element in a list

For:

NL = []
For I in L:
If I in NL:
Continue
Nl.append (i)

15, how to get the list of the intersection and the difference set

Answer: LIST=L1&L2

List=l1-l2

16, the following code will output what

def ex (val,list=[]):
List.append (Val)
Return list
List1 = EX (10)
List2 = EX (123,[])
List3 = EX (' A ')
Print (LIST1,LIST2,LIST3)
Answer: [Ten, ' a '] [123] [Ten, ' a ']

17. How to write variable parameters and keyword parameters in Python

A: Variable parameters are written after the keyword parameter

18. What is a lambda expression

A: Alambda function is a function that can receive any number of arguments (including optional parameters) and return a single expression value

19. What is the difference between the match of RE and search?

A: Match will be matched from the beginning and will fail with a different head, search would match the full text

20, 1 or 2 and 1 and 2 output what, why

Answer: Output

And the expression is evaluated from left to right in the #python, and if all values are true, the last value is returned, and if there is a false, the first false value is returned

21, 1 < (2 = = 2) and 1 < 2 = = 2 Output What, why

Answer: Flase,true Reason

Python pen question 1-21

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.