Python array, some operations of the dictionary

Source: Internet
Author: User

Beginners have just begun to learn python, if there is wrong or write bad place, please give us a lot of advice!
Output the current system in Python
Import Sys
Print (Sys.platform)
The output is currently in that directory
Import OS
OS.GETCWD ()
How many bits of a variable are output
s = ' spam '
Len (s)
Replace the value of a variable
s = ' Z ' + s[1:]
Find values within a variable
S.find (' pa ') #找到会返回1, could not be returned-1
Temporarily modify the value of a variable
S.replace (' pa ', ' xzc ') #将变量内pa临时替换成XZC
To run a file using exec
EXEC (open (' a.py;). Read ()) #能运行文件的最新版本
Python calculates the number of digits of a numeric value
Len (STR (2 * * 1000))
Python generates random numbers
Import Random
Random.random ()
Specify a range for the resulting random number
Random.random ([1,2,3,4])
Take out a place that fits into a division
A = ' aa,bb,cc,dd '
A.split (', ')
Remove the middle value at what begins with what ends
Import re
Match = Re.match (' hello[\t]* (. *) world ', ' Hello Python world ')
Match.group (1) #不匹配开头和结尾的值放到了group里边
Match = Re.match ('/(. *)/(. *)/(. *) ', '/usr/local/num ')
Match.Groups () #取出来/, show other values

List sequence Actions
Insert at last edge of array
L = [123, ' abc ', 1.23]
L.append (' ni ')
L.extend ([up])
L.insert (1, ' hello ') #在一的位置插入
To delete a value in an array
L.remove (123) #删除的时候要指定数值
L.pop (2) #python中的数组是从0 1 2 3 in this order
Del L[1] #删除第一位
Ascending order of arrays
m = [' 2 ', ' 3 ', ' 4 ', ' 1 ']
M.sort ()
Descending arrangement
m = [' 2 ', ' 3 ', ' 4 ', ' 1 ']
M.reverse ()

Array nesting
A =[[1,2,3],[4,5,6],[7,8,9]]
M[0][2] #结果输出3, reading the 2nd value in the first array

Python dictionary
D = {' Spa ': 2, ' Ham ': 1, ' Eggs ': 3}
Len (d) #查看d字典有几组值
Modify the value of a callout
d[' ham '] = [' Girl ', ' bak '] #在d数组中的修改ham的标注值
View dimension values only, and arrange them in reverse order
List (D.values ())
View all values of D
List (D.items ())
View callouts for a single point value
D.get (' Ham ') #只输出ham的标注
When a single point value does not exist
D.get (' aaaaa ', #当aaaaa不存在的时候), output 88
Dictionaries can be used to insert values in an updated way
D1 = {' Tom ': 4, ' Mum ': 5}
D.update (D1)
The deletion of the dictionary is the same
D.pop (' mum ') #如果 () If there is no content, the last one is deleted by default
You can specify the location of the character to delete
D.pop (1) #删除d组中的第二个值字典的排序是按照 0 1 sort of
A Python read dictionary can also be read in a variable way.
A = ' Spa '
b = d[a] #然后输出b的值就等于输出d字典中的 ' Spa ' value

Python specifies a dictionary that can be used to formulate
D = {}
D[99] = ' Spa '
d[' ham '] = 98
d[' lol '] = ' olk ' #新加的默认在最前边
Use if to detect whether the value is in the dictionary
if (' lol ') in D:
Print (d[(' lol ')])
Else
Print (0)
Insert a dictionary in the form of a zip function
D = dict (Zip ([' A ', ' B ', ' C '],[1,2,3])) #分别给相对应的位置来赋值a1 B2 C3.
Use for loops to assign values
D = {k:v for (k,v) in zip ([' A ', ' B ', ' C '],[1,2,3])}
operator &for loop to insert Dictionary
D = {X:X * * 2 for x in [1,2,3,4]}
D = {C:C * 4 for C in ' Spam '}
d= {c:c + '! ' for C in [' Spa ', ' ham ', ' DD ']} #在这里它默认会按照字母的顺序进行排列
Insert in a dict way
D = dict (a=1,b=2,c=3)
Remove the identity from the dictionary
K = D.keys ()
List (k)
Remove the dictionary value without the identity
v = d.values ()
List (v)
If you delete the values in the D dictionary, the values in K & V are removed.
Del d[' B ']
List (v)
List (k)
Novice has just begun to learn, if there is a wrong place, please give us a lot of advice!

This article is from the "Bucket Month" blog, please be sure to keep this source http://douyue.blog.51cto.com/10174393/1652462

Python array, some operations of the dictionary

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.