Python sequence split operator and map split operator instance

Source: Internet
Author: User

Use the sequence splitter operator (*) to provide positional parameters.

For example, the parameters of the function heron are stored in a list sides, which can be: Heron (sides[0],sides[1],sides[2]) can also be split: Heron (*sides). If the list contains more items than the function parameter, you can use the Shard to extract the appropriate parameters.

1. When using a function with a variable number of positional parameters , you can use the

The sequence split operator (*).

#1. At this point, args is a complete sequence, *args means that all elements in a sequence are split out as arguments to the function
def product (*args): result = 1 in args: result *=return result#2. When you pass a parameter, you should pass the split element that belongs to a sequence >>> product (1,2,3,4)->>> product (5,3)

You can place the keyword argument after the position parameter , Def sum_of_power (*args,poewer=1): ...

* itself as a parameter : Indicates that the position parameter should not appear after the *, but the keyword parameter is allowed: Def heron (a,b,c,*,unit= "meters"): ...

If you use * as the first argument , then no positional arguments are allowed and the keyword argument is used when calling the function: Def print_set (*,paper= "letter", Copies=1,color=false): .... You can call Print_set () without using any parameters, or you can change some or all of the default values. However, if you use positional parameters, you will produce typeerror exceptions, such as Print_set ("A4").

2. To split the mappings, you can use the

Map Split Operator (* *)

For example, use * * to pass a dictionary to the Print_set () function:

Options = Dict (paper= "A4", Color=true)

Pritn_set (**options)

The key value pairs of the dictionary are split, and the value of each key is given the appropriate parameter, with the same parameter name as the key.

Using * * In parameters, the created function can accept any number of keyword arguments given:

>>>def add_person_details (ssn,surname,**Kwargs):Print ("ssn=", SSN)Print ("Surname=", surname)For keyInchSorted (Kwargs):print ( "{0}={1} ".format (Key,kwargs[key])) >>> add_person_details ( 123, "luce", forename=< Span style= "COLOR: #800000" > "lexis", Age=47) ssn= 123 Surname= Luce Age=47 Forename=lexis >>> add_person_details (123," luce) ssn= 123surname= Luce         

Example of a sequence splitter and a map splitter:

#此处, args is a sequence, * indicates that args is split, so *args, as the first parameter of a function, indicates that the position of the first parameter of Print_args can be
#接受任意多个元素 (which belong to a sequence), for * *The same is true for Kwargs parameters.
>>>def print_args (*args, * *Kwargs):For I,argInchEnumerate (args):Print"Positional Argument{0}={1}". Format (I,ARG)For keyInchKwargs:print "keyword argument{0 }={1} ".format (Key,kwargs[key]) >>> Print_args ( ' a ", ' C ", ' d ", Name= ' tom", Age=12,sex= ' f ")

Results:

Example of a map split:

Name="Jack"age=24s="name is {name} and age was {age}". Format (* *locals ())print s         

Results:

Reference: Mark Summerfield. "Python3 Program Development Guide". 149-151.

Python sequence split operator and map split operator instance

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.