python自然语言处理-读书笔记6

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zlp_zky/article/details/83089874
# -*- coding:utf-8 -*-
# __author__ = 'lipzhang'

#4.1 回到基础
#赋值

#等式

#条件语句
#all()函数和any()函数可以应用到一个链表(或其他序列),来检查是否全部或任一项 目满足一些条件:
# sent = ['No', 'good', 'fish', 'goes', 'anywhere', 'without', 'a', 'porpoise', '.']
# print(all(len(w) > 4 for w in sent))
# print(any(len(w)> 4 for w in sent))

#4.2 序列
#for item in s          遍历 s 中的元素
#for item in sorted(s)  按顺序遍历 s 中的元素
#for item in set(s)     遍历 s 中的无重复的元素
#for item in reversed(s)            按逆序遍历 s 中的元素
#for item in set(s).difference(t)   遍历在集合s 中不在集合t的元素
#for item in random.shuffle(s)      按随机顺序遍历 s 中的元素
#我们可以在这些序列类型之间相互转换。例如:tuple(s)将任何种类的序列转换成一个 元组,list(s)将任何种类的序列转换成一个链表。我们可以使用 join()函数将一个字符串链 表转换成单独的字符串,例如:':'.join(words)。
# words = 'I turned off the spectroroute'.split()
# wordlens = [(len(word), word) for word in words]
# wordlens.sort()
# print(wordlens)
# print(' '.join(w for (_, w) in wordlens))
# import nltk
# #产生器表达式
# text = '''"When I use a word," Humpty Dumpty said in rather a scornful tone,"it means just what I choose it to mean - neither more nor less."'''
# print(max(w.lower() for w in nltk.word_tokenize(text)))

#风格的问题
#函数:结构化编程的基础
# import re
# def get_text(file):
#     """Read text from a file, normalizing whitespace and stripping HTML markup."""
#     text = open(file).read()
#     text = re.sub('\s+', ' ', text)
#     text = re.sub(r'<.*?>', ' ', text)
#     return text

#函数的输入和输出
#变量的作用域:名称解析的LGB 规则:本地 (local),全局(global),然后内置(built-in)

#参数类型检查
# def tag(word):
#     assert isinstance(word, str), "argument to tag() must be a string" #使用 assert 语句和 Python的 basestring 的类型一起,它是 uni code和 str的产生类型。 如果assert 语句失败,它会产生一个不可忽视的错误而停止程序执行。
#     if word in ['a', 'the', 'all']:
#         return 'det'
#     else:
#         return 'noun'


#python库的样例
#Matplotlib 绘图工具
#NetworkX
# import networkx as nx
# import matplotlib
# from nltk.corpus import wordnet as wn
# def traverse(graph, start, node):
#     graph.depth[node.name] = node.shortest_path_distance(start)
#     for child in node.hyponyms():
#         graph.add_edge(node.name, child.name)
#         traverse(graph, start, child)
# def hyponym_graph(start):
#     G = nx.Graph()
#     G.depth = {}
#     traverse(G, start, start)
#     return G
# def graph_draw(graph):
#     nx.draw(graph, node_size=[16 * graph.degree(n) for n in graph], node_color=[graph.depth[n] for n in graph],
#                      with_labels=False)
#     matplotlib.pyplot.show()
# dog = wn.synset('dog.n.01')
# graph = hyponym_graph(dog)
# graph_draw(graph)
from numpy import array
cube = array([ [[0,0,0], [1,1,1], [2,2,2]],[[3,3,3], [4,4,4], [5,5,5]],[[6,6,6], [7,7,7], [8,8,8]] ])
print(cube[1,1,1])
from numpy import linalg

a = array([[4, 0], [3, -5]])
u,s,vt = linalg.svd(a)#矩阵的svd分解
print(u)
print(s)
print(vt)

猜你喜欢

转载自blog.csdn.net/zlp_zky/article/details/83089874