20,190,826 jobs

20,190,826 jobs

1. List role of digital type, string type, list, dictionary definition and usage mode respectively

Digital Type

1. Role

Digital record id, phone number

2. Define the way

int / or direct assignment

zhengxing = 123

3. Use

x = 1
y = 2
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)  # 取余
print(x // y)  # 取整
print(x ** y)  # 幂
import cmath  # 科学计算器
# cmath.log()
# cmath.cos()
print(cmath.pi)   #输出π
print(cmath.e)  # 自然底数

String type

1. Role

Name / Sex

2. Define the way

name = 'nick'  # 单引号内把字符串起来
name1 = "nick"

3. Use

(内置方法---》只有字符串才能使用,其他数据类型无法使用)

例子:

s='nick handsome'

1.startwith:以。。。开始

print(s.startswith('nick'))    #ture      s这一变量是否以Nick为开始



2.endswith:以。。。结束

print(s.endswith('a'))        #false      s这一变量是否以a结束  

Index values

poem = 'Rice said floral harvest, listening to the frogs'

0123456789 describes a position of a character index, starting from 0, the flashback is -1

print(poem[0])     #输出  稻
print(poem[-1])     #输出  片

Index sliced

print(poem[0:7])     #输出0-6   顾头不顾尾    稻花香里说丰
print(poem[2:4])     #输出2-3       香里

空白
print(poem[:5])      #输出第一位字符到第4个     稻花香里
print(poem[8:])     #输出第8位字符直到最后     听取蛙声一片

Steps

Several character spacing values

:print(poem[0:7:2])    #输出    稻香说
   
# jntm="基尔是一名来自尼泊尔的僧人光太郎,走访到过美国英国法国德国等西方国家"[0:22:7]
# print(jntm)
#基尼太美
    

List

1. What is a list

A character can only store a value for storing a plurality of values ​​(hobby)

2. Define the way

With [] storage, a plurality of spaced elements (of any data type) with a comma

hobby_list=['chang','tiao','rap','lanqiu', ['jntm','cxk']]
                0      1      2      3             4
                                                0      1
        
        
 输出为列表形式    

3. Value Index

print (hobby_list[2])         #取变量的第二位元素     即  ['rap']

print (hobby_list[4][0])      #取 变量hobby第4位元素中的0位数值
                               #即  jntm
    
    

4. Slice

print(hobby_list[0:4])       #取0-3元素即                                                      #['chang','tiao','rap','lanqiu']
print(hobby_list[:4])     # 前面如果没写,默认为0
print(hobby_list[0:])   # 后面如果没写,默认为最后一个
print(hobby_list[:])     #   默认为第一到最后

The built-in method (the append)

In the last element will append a plus

A. Recall

hobby_list=['chang','tiao','rap','lanqiu', ['jntm','cxk']]
hobby_list.append('read')
print(hobby_list)     #j结果为['chang','tiao','rap','lanqiu', ['jntm','cxk'],'read']

6. Modify Index

Replace the direct assignment

单个元素替换
hobby_list[0]='sing'
print(hobby_list)        
#结果为['sing','tiao','rap','lanqiu', ['jntm','cxk']]


整体替换
hobby_list[:] = 1,1,1,1,1,1,  # 仅作了解  (有几个值替换几个)
print(hobby_list)
#结果为  [1,1,1,1,1,1]

dictionary

1. Why do you need dictionary

信息    物种    性别 H   W            爱好
xinxi=['ren','nan',180,200,['sing','read','look']]
         0     1    2   3              4
    print(xinxi[2])    #输出180    可是不利于寻找
    


2. What is the key to

key: value corresponding to the key value: {} separated by comprising

3. Dictionary

a. dictionary print and modify (del)

{} Have a plurality of keys (typically a string having a meaning described) values ​​(specific values, the value of any data type)

xinxi={'wuzhong':'ren','xingbie':'nan','w':180,'H':200,
       'aihao ':['sing','read','look']}
     
    print(xinxi)  
    #结果为:{'wuzhong':'ren','xingbie':'nan','w':180,'H':200,
      #      'aihao ':['sing','read','look']}
        
  1.  也可单独打印
    
    print(xinxi['w'])      #结果为 180
    print(xinxi['aihao'])   #结果   ['sing','read','look']
    
  2.修改值
  xinxi['H']= xinxi['H']+1    # 201
  print(xinxi['H'])          #结果为 201


  3.删除值(del)
    del xinxi['w']         #删去 xinxi变量中 'w'元素
    print(xinxi)
#输出为   {'wuzhong':'ren','xingbie':'nan',H':200,
      #      'aihao ':['sing','read','look']}
b. Alternatively incision (Split)

.split call

s='a+b+c+d'
print(s.split('+')) 
#  按照 + 把字符串切开,然后把切开的元素放入列表
#结果    ['a', 'b', 'c', 'd']

c. stitching (join)

.join call

lis=['a','b','c','d']    # 定义列表
res='+'.join(lis)        # 按照字符串把列表中每一个元素取出来拼接
 print(res)              #结果是  a+b+c+d   输出字符串

d. generating a list (list)

.list call

s1='nick abc'
print(list(s1))
#输出结果为   ['n','i','c','k',' ','a','b','c']

2. Construction of a word cloud

s="汉字的顺序不影响阅读 abc"
w = wordcloud.WordCloud()      # 生成一个词云对象  
w.generate(s)                #在词云中放文本
w.to_file('nick1.png')         #生成图片

#生成图片汉字会出现乱码,外国人写的库默认支持英文,需找中文字体(c盘windows里fonts,找到并复制路径)
1.重新编码
w = wordcloud.WordCloud(font_path=汉字字体路径)    # 生成一个词云对象 
                       .ttc格式

                        
2.使用jieba库将词云中的语句分割开
s='nick是我们的预科班教程老师'
s_list=jiba.lcut(s)      #将变量字符串s分割并生成列表
   #['nick','是','我们','的','预科班','教程老师']
print(' '.join(s_list))   #列表需要改为字符串,用.join空格分开
   # nick 是 我们 的 预科班 教程老师    
                        
                        
                       

Guess you like

Origin www.cnblogs.com/fwzzz/p/11414484.html