Python _ Sequence and ing unpack operations, python sequence ing

Source: Internet
Author: User

Python _ Sequence and ing unpack operations, python sequence ing

UnpackEach element in the sequence or ing is extracted separately. A simple usage of sequence unpacking is to extract the first or first element and the following elements, for example:

First, seconde, * rest = sequence

If there are at least three elements in sequence, after executing the above Code, first = sequence [0], second = sequence [0], rest = sequence [2:].

Uncertain parameter received by function

When the parameter of the function is unknown, you can use * args and ** kwargs. * args has no key value, and ** kwargs has a key value.

#! /Usr/bin/python #-*-coding: UTF-8-*-import sysreload (sys) sys. setdefaultencoding ('utf-8') ''' when the parameter of the function is unknown, you can use * args and ** kwargs. * args has no key value, and ** kwargs has a key value. '''Def fun_var_args_kwargs (data1, * args, ** kwargs): print 'data1: ', type (data1), data1print' * args: ', type (args ), argsprint '** kwargs:', type (kwargs), kwargsfun_var_args_kwargs ('this is data1 ', 2, '3', 4.0, k1 = 'value1 ', k2 = 'value2') print '----------- 'def print_args (* args, ** kwargs): print args. _ class __. _ name __, args, kwargs. _ class __. _ name __, kwargsprint_args () print_args (1, 2, 3, a = 'A ')

Print result:

Data1: <type 'str'> this is data1
* Args: <type 'tuple'> (2, '3', 4.0)
** Kwargs: <type 'dict '> {'k2': 'value2', 'k1': 'value1 '}
-------------
Tuple () dict {}
Tuple (1, 2, 3) dict {'A': 'A '}


How does python unpack c except struct?

You can use cython for programming. In this way, the C language is used up and put in the PYTHON structure. In addition to struct, I can't think of any other methods.

Unpack python Parameters

Argv is a tuples. The first parameter is the script file name !! Therefore, you need to enter two parameters.
Filename, m, n = argv

Then 7.py 2 3
Then we can get m = 2, n = 3!

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.