Word cloud data types and map production

day04

A data type

1. Integer

Define integer:

  • a = 8

  • a = int(8)

    a = float (8.9) print (a) the result is: 8

2. Float

Float is defined:

  • a = 1.6

  • a =float(1.6)

    a = float (5) print (a) the result is: 5.0

3. string type

(1) defined by:

  • (Double single quotes) == 'content' ==; (double quote) == "Content" ==; (three double quotes) == '' 'content' '' ==, no single quotes character string together called variable name
  • When a string contains a single quotes double quotation marks Definition: st = "all of us I'm good." Similarly when a character string included in double quotation marks defined by single quotes when a string contains a single and double quotes, defined by three marks (here, three marks may also be used for multi-line comments)

Some methods (2) built-in string, a string can be used only == ==

As String: s = 'hou shao wu'

  1. stratwith begin with ...
print(s.startwith('hou'))   #结果:  True
  1. endwith to end ...

    print(s.endwith('wu'))   #结果:  True

(3) Method of use: the index value, the index modify value, the slice index

Index values

Elements in the list, the header onward, has a corresponding index (i.e., index), is a sub-table hobby_list [0 ~ -1], i.e. the first element is 0, the second is 1, the first countdown element is -1, -2 is the penultimate, etc.

== Basic syntax: ==

  print(字符串名[下标数])

4. Slice Index

== Basic syntax: ==

字符串名[:]   #遍历字符串
字符串名[下标数1:下标数2]   #从下标数1到下标数2
字符串名[下标数1:下标数2:步数]  #从下标数1到下标数2,步数为每次前进的位数

4. List

(1) defined by:

With == [] == stored, wherein the comma (which may comprise any number of types), can be inserted into the list of the additional list, which is used as an element.

Such as:

hobby_list = ['da','xiao','zhong',500,['sda','600','thf'],'sdxcdef']

(2) Method: index value, modify the value of the index, the index slice

  • Index values

Elements in the list, the header onward, has a corresponding index (i.e., index), is a sub-table hobby_list [0 ~ -1], i.e. the first element is 0, the second is 1, the first countdown element is -1, -2 is the penultimate, etc.

print(hobby_list[4])    #结果为:['sda','600','thf']
print(hobby_list[4][0])   #结果为:sda

No single quotes brackets and output a single element. However, when the output result is two or more elements 2, the output in list form

  • Modify the value of the index

== directly modify an assignment ==

  1. Modify the individual elements:
hobby_list[0] ='good' #输出是:hobby_list = ['good','xiao','zhong',500,['sda','600','thf'],'sdxcdef']

The first element of the original list hobby_list da, it has become good

  1. Modifying the plurality of element: The following list
hobby_list[:] = 0,1,2,3,4,5 # 仅作了解
  • Index sliced

== slice output from a few to a few, but regardless of Gu head tail ==

== Basic syntax: ==

列表名[:]
列表名[下标数1:下标数2]   #从下标数1到下标数2
列表名[下标数1:下标数2:步数]  #从下标数1到下标数2,步数为每次前进的位数
print( hobby_list[0:4])   #结果为: ['da','xiao','zhong',500] #但不包括下标为4的元素。

[:]: It shows a full output, both before and after the default 0

[0: -1] and [: -1]: represents the output of all except the last one

(3) some built-in method of the list, the list can be used only == ==

  • append (bonus)
print(hobby_list.append('read')) #在列表hobby_list尾部加上一个字符串  read

#结果:hobby_list = ['da','xiao','zhong',500,['sda','600','thf'],'sdxcdef','read']

5. Dictionary

  • Dictionary is the key to key: value form consisting of

Such as:

yy_info_dict = {'name': 'jinyuyang', 'height': 180 , 'weight': 150, 'hobby_list': ['jiao', 'jump_with_fengge', 'dapao']}

(1) Usage: Press the key value, del delete the value

  1. Press the key values:
print(yy_info_dict['name'])  # 按key取值 结果:jinyuyang
print(yy_info_dict['weight'])  # 按key取值 

yy_info_dict['height'] =151  # 151  # 按key修改值
print(yy_info_dict)
  1. del Delete value

    del yy_info_dict['height']
    

    Second, the word cloud map making

    import wordcloud
    import  jieba
    from imageio import imread
    
    mk = imread('c.png')#选择外部特定的图框
    
    t =open('2.txt','r',encoding="utf-8")#读取外部文字文件
    s=t.read()
    t.close()
    
    s_list=jieba.lcut(s)   #把字符串切割成列表
    print(s_list)
    s = ' '.join(s_list)    #把列表拼接成字符串,这里是以空格把列表拼接成字符串
    print(s)
    w = wordcloud.WordCloud(width = 1000,height = 1000,font_path ='C:\Windows\Fonts\simsun.ttc',mask=mk,background_color='black')
    #上面的方法的作用是:规定了词图云的尺寸,词语分类的词库,词图框为mk,词图云背景颜色为黑
    
    w.generate(s)
    w.to_file('qiyu.png')

Guess you like

Origin www.cnblogs.com/Mcoming/p/11414467.html