03 data types and the word cloud

type of data

Data type formed

Variables: describe things or things in the world of state property

In order to describe the state of things in the world, and therefore have a data type: data classification

Digital Type

Integer type (int)

  1. Role: represents the ID / telephone number, etc.
  2. Defined way (python and reality)
herry_id = 33038
herry_id = int(33038)
  1. Instructions
    x = 10
    y = 20
    print(x+y) #算术运算,逻辑判断
    print(x**y) #幂方
    print(x//y) #取整
    import cmath #计算器
    cmath.log()
    cmath.cos()
    print(cmath.pi) #π
    print(cmath.e) #E

Floating-point type (float)

  1. Role: represents pay 3.1w, height, weight, etc.
  2. Defined way
salary = 3.1
print(salary)
salary1 = float(3)#强制类型转换(动态语言)print(salary1)  #3.0
  1. Instructions

    Use the same type integer arithmetic and logic operations support

String type

A plurality of character string (or null character) consisting of

  1. effect

    Indicate the name, hobby

  2. Defined way

name = 'herry' #单引号内把字符串起来
name = "herry"
height = 'herry height' #读到第一个引号开始,字符串开始
#第二个引号结束,字符串结束
# name #没有单引号串起来的字符  叫变量名

Long strings are defined:

# 三引号,换行,
repm = '' #空字符串
poem = '''这
是
一
首
诗
'''
#字符串开始符是''',结束也必须是''',中间''或者'没关系
#"""同理
  1. Instructions

    Type and numeric string str and int the method

    name2 = str('herry')
    height_str = '180' #字符串
    height =int('180')
    print(height_str,type(height_str))
    print(height,type(height))
    height_int = 180
    height = str(height_int)
    print(type(height))

    String type only + * (essentially a string concatenation) and the logical comparison (comparison performed by ASCLL code)

    name = 'herry'
    name2 = 'herry1'
    print(name+name2)#herryherry1
    
    print(name*2)#herryherry

    Built-in process (a string can be used)

    1. startswith 以...开始
    1. endswith to end ...
    s = 'herry handsome'
    print(s.startswith('herry')) # Ture ->正确
    print(s.endswith('a')) #flase 错误
    1. Index values
    poem2 = '''这又是一首诗,我么得骗你'''
    #          0 1 2 3 4 5 6 7 8 9 
    #  -> 描述了字符的位置,从0开始
    #                     -4-3-2-1
    print(poem2[2]) #是
    print(poem2[-2]) #骗
    # 索引切片
    print(poem2[2:8]) # 2-8个 ,顾头不顾尾
    # 步长
    print(poem2[2:8:2]) #是首,
    
    jntm="基尔是一名来自尼泊尔的僧人光太郎,走访到过美国英国法国德国等西方国家"[0:22:7]
    print(jntm) #基尼太美
    

List

A string that can store a value used to keep a list of a plurality of values

  1. Action represents something more for the same property (hobbies, favorite foods)
  2. Defined way
# 用[]存储,用.隔开
 hobby_list = ['chang','tiao','Rap',['22','656'],'速度']
 hobby_list2 = ['jiao']

3. Use

#索引取值
hobby_list3 = ['chang','tiao','Rap',['22','666'],'速度']
#                0       1      2       3     
print(hobby_list3[3][1])#666
# 切片
print(hobby_list[0:4:2])#chang Rap
print(hobby_list[:4]) #如果没写,默认0
print(hobby_list[0:-1]) #如果没写,默认最后一个
print(hobby_list[:])

#内置方法
hobby_list3.append('我加了一个值')
print(hobby_list3)

#索引修改值
hobby_list3[0] = '唱'
print(hobby_list3)
hobby_list3[:] = 1,2,1,2
print(hobby_list3)

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

#拼接
lis = ['a','b','c','d']
res = '+'.join(lis) # 按照字符串吧列表里每一个元素取出来拼接

print(res)#a+b+c+d

dictionary

To the values ​​in the list, add the details

hr_info_list = 
['hongrui',180,180,['chang','tiao','rap']]
# name    height weight  hr_hobby
#  0        1    2           3
print(hr_info_list[1])
  1. effect

    A plurality of values ​​used to access, in accordance with key: value of the stored-value mode, may not take the time to go to the index value by the key, key has a function of descriptive value.

  2. Defined way

    # 字典的本质是 键值对
    # 键  :  值
    # key : value
    # 字典:{}内有多个键(一般为字符串,具有描述意义)  值(具体的值,值为任意类型)
    dic = {'name':'herry','height':180,'weight':180,'hobby_herry':['chang','tiao','rap']}
    print(dic)
  3. Use

    print(dic['name'])# 按key取值
    
    dic['weight'] = 150 #按key修改值
    print(dic)#herry
    #del删除值
    del dic['weight']
    print(dic)

Word cloud

jieba module (stuttering)

For slicing string, word by segmentation, cut into the list

import jieba
s = '说话一段一段的'
print(jieba.lcut(s))
#['说话', '一段', '一段', '的']

imread module (ImageIO)

Used to read the disk image into memory

mk = imread('绿油油.png')

wordcloud module

By .join method jieba cut into a string list reassemble

Access mask set up in memory of imported pictures

Use built-in way to generate word cloud wordcloud

s_list = jieba.lcut(s) #把字符串切割成列表
#print(s_list)
s = ' '.join(s_list) #把列表拼接成字符串
w = wordcloud.WordCloud(width=1000,height=1000,font_path=r'C:\Windows\Fonts\msyhbd.ttc',background_color='white',mask=mk) #生成一个对象
w.generate(s)
w.to_file('five.png')

Guess you like

Origin www.cnblogs.com/Henry121/p/11414464.html