


Introduction to map() and zip() operation methods in python
For map(), its prototype is: map(function, sequence), which is to perform function operations on each element in the sequence.
For example, the previous a, b, c = map(int, raw_input().split()) means to convert the input a, b, c into integers. Another example:
a = ['1','2','3','4'] print map(list,a) print map(int,a)
The first map converts each element in list a into a list, and the second map converts each element in a is an integer.
For zip(), the prototype is zip(*list), list is a list, and zip(*list) returns a tuple, such as:
list = [[1,2,3],[4,5,6],[7,8,9]] t = zip(*list) print t
Output: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
x = [1,2,3,4,5] y = [6,7,8,9,10] a = zip(x,y) print a
Output: [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
Here are some additions:
[python] >>> list = [[0,1,2],[3,1,4]] >>> [sum(x) for x in list] [3, 8] >>> map(sum,list) [3, 8]
If you want to get the sum of each column, you need to use zip(*list) to unzip the list first and get a tuple list, in which the i-th element The group contains the i-th element of each row:
[python] >>> list = [[0,1,2],[3,1,4]] >>> zip(*list) [(0, 3), (1, 1), (2, 4)] >>> [sum(x) for x in zip(*list)] [3, 2, 6] >>> map(sum,zip(*list)) [3, 2, 6]
The following example is about how zip and unzip (actually zip and * are used together) work :
[python] >>> x=[1,2,3] >>> y=[4,5,6] >>> zipped = zip(x,y) >>> zipped [(1, 4), (2, 5), (3, 6)] >>> x2,y2=zip(*zipped) >>> x2 (1, 2, 3) >>> y2 (4, 5, 6) >>> x3,y3=map(list,zip(*zipped)) >>> x3 [1, 2, 3] >>> y3 [4, 5, 6]
For more related articles introducing the operation methods of map() and zip() in python, please pay attention to the PHP Chinese website !

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

application.yml defines the list collection. The first way is to use the @ConfigurationProperties annotation to obtain all the values of the list collection type:code:status:-200-300-400-500. Write the entity class corresponding to the configuration file. What needs to be noted here is that defining the list Collection, first define a configuration class Bean, and then use the annotation @ConfigurationProperties annotation to obtain the list collection value. Here we will explain the role of the relevant annotations. @Component hands over the entity class to Spring management @ConfigurationPropertie

1. Technical background In actual project development, we often use caching middleware (such as redis, MemCache, etc.) to help us improve the availability and robustness of the system. But in many cases, if the project is relatively simple, there is no need to specifically introduce middleware such as Redis to increase the complexity of the system in order to use caching. So does Java itself have any useful lightweight caching components? The answer is of course yes, and there is more than one way. Common solutions include: ExpiringMap, LoadingCache and HashMap-based packaging. 2. Technical effects to realize common functions of cache, such as outdated deletion strategy, hotspot data warm-up 3. ExpiringMap3.

Compressing HTML files into ZIP can improve page loading speed. Methods include: using online tools (such as FileOptimizer, TinyPNG) using command line tools (such as gzip, 7-zip) using Node.js scripts (using the zlib module)

1. The compressed folder is a zip file [root@cgls]#zip-rmydata.zipmydata2. Unzip mydata.zip into the mydatabak directory [root@cgls]#unzipmydata.zip-dmydatabak3.mydata01 folder and mydata02.txt are compressed into mydata.zip[root@cgls]#zipmydata.zipmydata01mydata02.txt4. Decompress the mydata.zip file directly [root@cgls]#unzipmydata.zip5. View myd

There are many ways to convert javabeans and maps, such as: 1. Convert beans to json through ObjectMapper, and then convert json to map. However, this method is complicated and inefficient. After testing, 10,000 beans were converted in a loop. , it takes 12 seconds! ! ! Not recommended. 2. Obtain the attributes and values of the bean class through Java reflection, and then convert them into the key-value pairs corresponding to the map. This method is the second best, but it is a little more troublesome. 3. Through net.sf.cglib.beans.BeanMap Method in the class, this method is extremely efficient. The difference between it and the second method is that because of the use of cache, the bean needs to be initialized when it is first created.

The zip command is a very useful compression tool in Linux systems. By using the zip command, you can easily compress files and directories into a zip file and save storage space and facilitate transfer. The basic syntax of the zip command is "zip [options] [compressed file name] [file or directory to be compressed]".

Optimizing the performance of Go language map In Go language, map is a very commonly used data structure, used to store a collection of key-value pairs. However, map performance may suffer when processing large amounts of data. In order to improve the performance of map, we can take some optimization measures to reduce the time complexity of map operations, thereby improving the execution efficiency of the program. 1. Pre-allocate map capacity. When creating a map, we can reduce the number of map expansions and improve program performance by pre-allocating capacity. Generally, we

Method 1. Use HashtableMapashtable=newHashtable(); This is the first thing everyone thinks of, so why is it thread-safe? Then take a look at its source code. We can see that our commonly used methods such as put, get, and containsKey are all synchronous, so it is thread-safe publicsynchronizedbooleancontainsKey(Objectkey){Entrytab[]=table;inthash=key.hashCode( );intindex=(hash&0x7FFFFFFF)%tab.leng
