Python-functional programming, List-generation

Source: Internet
Author: User

The structure of centrally stored data that is common in 1.python:

List

Collection

Dictionary

Meta-group

String

Dual queue

Heap

One of the most common is the list, dictionary.

2. Here are some elements that use loops to get a list of dictionaries

1>>> dic={'name':'Zhangsan',' Age': 24,' City':'Jinhua'}2>>> forKey,valueinchDic.items ():3     Print(Key,value)4 5     6 name Zhangsan7Age 248City Jinhua

Loop Get list

>>> lists=[1,2,3,4,5] for in lists:    item+1    23456

Some introduction to 3.python functional programming

Some of the functions of Python about function programming are:

Map (function,list), map function

Filter (), filtering function

Reduce (), statute function

Lambda functions

List-Generated

1 def Inc (X):return x+12 >>> list (map (inc,lists))3 [2, 3, 4, 5, 6 ]4 use a lambda expression for a function, abbreviated to one line of code 5 >>> items=[1,2,3,4]6 >>> list (map (Lambda x:x+1), items)7 [2, 3, 4, 5]
use of the filter function >>> list (filter (lambda x:x<3), items) [1, 2]
1 The reduce function needs to import the reduce module 2  from Import Reduce 3 >>> reduce ((lambda x,y:x/y), items)4 0.0416666666666666645  The last concept of functional nominal is the list-generated 6 for in range (3)]7 > >> s8 [0, 1, 4]

Python-functional programming, List-generation

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.