What are the commonly used type conversion functions and sequence functions of python-?

Source: Internet
Author: User
Tags mul ord

First, type conversion function

chr (i)
The CHR () function returns a string corresponding to the ASCII code.
Print Chr (A)
Print Chr (+) B
Print Chr (+CHR) AB

Ord (x)
The Ord () function returns the ASCII or Unicode value of a string parameter.
Ord ("a") 97
Ord (U "a") 97

Hex (x)
The hex () function converts integers to hexadecimal numbers.
Hex (+) ' 0x10 '
Hex (123) ' 0x7b '

Oct (x)
The OCT () function converts an integer given to an octal number.
$amp;>amp;>amp; $gt; Oct (8) ' 010 '
$amp;>amp;>amp; $gt; Oct (123) ' 0173 '

Long (x[,base])
The long () function converts numbers and strings to grow integers, and base is an optional cardinality.
Long ("123") 123L
Long (one) 11L

float (x)
The float () function converts a number or string into a floating point.
Float ("12") 12.0
Float (12L) 12.0
Float (12.2) 12.199999999999999
Float (12.3) 12.300000000000001
Float (12.4) 12.4

Int (x[,base])
the int () function converts numbers and strings into an integer, base is an optional cardinality.
Int (3.3) 3
Int (3L) 3
Int ("13") 13
Int ("14", 15) 19

list (x)
The list () function converts a sequence object to a list.
List ("Hello World") [' H ', ' e ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ']
List ((1,2,3,4)) [1, 2, 3, 4]

str (obj)
The STR () function converts an object into a printable string.
STR ("4") ' 4 '
STR (4) ' 4 '
STR (3+2J) ' (3+2j) '

tuple (x)
The tuple () function converts a sequence object to a tuple.
Tuple ("Hello World") (' h ', ' e ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ')
Tuple ([1,2,3,4]) (1, 2, 3, 4)

second, the sequence of common functions

filter (function,list)
When you call filter (), it applies a function to each item in the sequence and returns all items when the function returns True, thereby filtering out all items that return false values.
def Nobad (s):
... return s.find ("bad") = =-1
...
s = ["bad", "good", "bade", "we"]
Filter (nobad,s) [' Good ', ' we ']
This example filters out all items that contain "bad" by applying the Nobad () function to all the items in the S sequence.

map (function,list[,list])
The map () function applies a function to all items in the sequence and returns a list.
Import string
s=["Python", "Zope", "Linux"]
Map (string.capitalize,s) [' Python ', ' Zope ', ' Linux ']
Import operator
s=[1,2,3]; t=[3,2,1]
Map (operator.mul,s,t) [3, 4, 3]
If you pass a none value instead of a function, map () merges the corresponding elements in each sequence and returns that tuple.
a=[1,2];b=[3,4];c=[5,6]
Map (NONE,A,B,C) [(1, 3, 5), (2, 4, 6)]

reduce (Function,seq[,init])
The reduce () function obtains the first two items in the sequence, passes it to the provided function, obtains the result, and then passes the next item in the sequence, along with the result, to the function, and so on, until all items have been processed.
Import operator
Reduce (operator.mul,[2,3,4,5]) # (2*3) 120
Reduce (operator.mul,[2,3,4,5],1) # ((1*2)) 120
Reduce (Operator.mul, [2,3,4,5],2) # ((2*2)) 240

zip (seq[,seq,...])
The zip () function merges the corresponding items in two or more sequences and returns them in a tuple format, stopping after all items in the shortest sequence have been processed.
Zip ([1,2,3],[4,5],[7,8,9]) [(1, 4, 7), (2, 5, 8)]
If the argument is a sequence, zip () returns each item in a tuple format.
Zip ((1,2,3,4,5)) [(1,), (2,), (3,), (4,), (5,)]
Zip ([1,2,3,4,5]) [(1,), (2,), (3,), (4,), (5,)]

What are the commonly used type conversion functions and sequence functions of python-?

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.