Python Foundation 13_zip_import

Source: Internet
Author: User
Tags pow

Continuing with the built-in functions, the zip function is likened to a zipper that corresponds to the teeth on both sides.

#!/usr/bin/env python#Coding:utf-8#The analogy is like a zipper, which corresponds to both sides. #more out of the part, not for processing. Print(List (Zip ('a','b','C'), (11,22,33))))Print(List (Zip ('a','b','C'), (11,22,33,44))))Print(List (Zip ('a','b','C','D'), (11,22,33,))))#splitting the elements of a dictionaryDIC = {'name':'Tom',' Age': 22,'Gender':'male'}Print(list (Zip (Dic.keys (), dic.values () )))Print(List (Zip ('ABCDE','123456')))## Use the Zip method to handle the largest set of values in a dictionaryage_dic={'Tom_age': 81,'Jerry_age': 133,'Louis_age': 88,'Bob_age': 55}Print(Max (Age_dic.values ()))Print(Max (Zip (age_dic.values (), Age_dic.keys ())))#by turning V and K upside down, it's easy to use Max. Dic2=(    {'name':'Tom',' Age': 33},    {'name':'ABC',' Age': 88},    {'name':'Bob',' Age': 66},    {'name':'Jim',' Age': 44},)#Max can use the key method to pass a function in. #equivalent to the tuple or list for a For loop processing. Print(Max (Dic2, key=Lambdaitem:item[' Age']))

There are several other built-in functions:

#!/usr/bin/env python#Coding:utf-8Print(Pow (3,3))Print(Pow (3,3,4))#The third parameter is the remainder after 3**3a='python'S1= Slice (3,5)#Slice function#print (A[3:5])Print(a[s1]) S2=slice (3,6,2)#Step size is 2Print(A[s2]) b= [3,2,1,7,5]Print(sorted (b))#sort functionDiC= {'K11': 3000,'K6': 5000,'K3': 6000,'K4': 8000,'K2': 7000}#print (sorted (DIC))Print(Sorted (dic,key=LambdaK:DIC[K]))#sort by the value of a dictionaryPrint(Sorted (Zip (dic.values (), Dic.keys ()))#get the value sorted by value and keyPrint(sum (b))Print(Sum (range (5)))#from 0 to 4!Print(List (range 5)) msg='123'ifType (msg) isStr:#Type is used to determine variable types    Print(int (msg) +2)defTest (): a='ert'    Print(Locals ())#print variables in the current scope, in dictionary form    Print(VARs ())#when no parameters are equal to locals ()Test ()

and import and __import__.

Contents of the test.py file:

# !/usr/bin/env python # Coding:utf-8 def func ():     Print ('hello,python')

Content of test2.py:

# !/usr/bin/env python # Coding:utf-8 def ABC ():     Print ('aaaaaaaaaaaaaa')

Next, compare the usage differences between the two types of import

#!/usr/bin/env python#Coding:utf-8ImportTest#Import the test.py module, which is to import the contents of this file. Test.func ()#using functions in the test module## __import__ is the module that imports the string name form#m_name = ' test2 'm =__import__('test2')#To import a module named after a string, you must assign a value to the variablem.abc ()## # import >> sys >> __import__## import will call SYS, or use the __import__ method at the bottom. 

These are notes, may seem a bit messy, and so on after more and more familiar after the careful sorting.

Python Foundation 13_zip_import

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.