码迷,mamicode.com
首页 > 编程语言 > 详细

python---10月

时间:2018-11-06 22:30:45      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:这一   实现   子节点   lang   monitor   mat   比较   gcc   ubunt   


7. Reverse Integer

将整数翻转。 注意123000翻转为321 -123 翻转为-321

方法7. if x < 0:
x = -1 * int( str(0 - x)[::-1] )
else:
x = int( str(x)[::-1] )


转为字符串形式, x0 时 先 0-x 变为整数。

注意: int 可以直接把 整数前面 的0 去掉。


Python3 中比较

Import operator

operator.lt(a, b) less than

operator.le(a, b)

operator.eq(a, b)

operator.ne(a, b)

operator.ge(a, b)

operator.gt(a, b)


再次复习range的用法:

Range5) 则 为0 4

Array = [1,2,3,4,5]

Array[0:] 从标号0 开始

Array[:-1] 列出-1 之前的。:代表省略的

4 3 2 1


Array3-3】:


两个 : 表示 以某个step 跳步

array【::2】 冒号在前面。

range(6,1,-1)思是从61间隔-1-1的意思是倒着数。

2018101019:29:04

通俗的理解__name__ == ‘__main__‘:假如你叫小明.py,在朋友眼中,你是小明(__name__ == ‘小明‘);在你自己眼中,你是你自己(__name__ == ‘__main__‘)

if __name__ == ‘__main__‘的意思是:当.py文件被直接运行时,if __name__ == ‘__main__‘之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == ‘__main__‘之下的代码块不被运行。


Lee27 remove element

# #注意读懂题目
# Given an array nums and a value val,
# remove all instances of that value in-place and return the new length.
# Do not allocate extra space for another array,
# you must do this by modifying the input array in-place with O(1) extra memory.
# The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

#
只要求返回长度
class Solution(object):
def
removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
length = 0
for i in xrange(len(nums)):
if
nums[i] != val:
nums[length] = nums[i]
length
+= 1
return length

比如 【1 2 2 4 5 】 运行后为 【1 2 5



Xrange 区别range

Xrange 使用生成器,占用的空间小。

defremoveElement(self, nums, val):

while val innums:

nums.remove(val)

return len(nums)




20181022

pycharm 中自定义了一个user

在其他地方进行实例化时, newUser = user() user下方总是有红色的下划线。 这种情况未必是错误,也可能是因为命名,格式等不符合规范。 将类名改为 User,首字母大写,问题解决。

另外,newUser = user() user下方有红色下划线,也可以去类user下看情况的。


很多时候,报错在一句。当你怎么都看不出来错误的时候,可以看看上一句。说不定错误就在那里呀。


四、python知识

4.1heapq堆的使用

假设需要维护一个列表,这个列表不断有新的元素加入,你需要在任何时候很方便的得到列表中的最大()值,因此要求列表始终处于排序完毕状态,。

一个最简单的方法:每次插入新的数据时,调用一次sort方法,这样可以保证列表的顺序。

在数据量很小的情况下,这种方法可行。但是,sort方法实现并不高明,采用了自然归并排序,虽然排序开销已经被尽量的压缩了,复杂度大概是O(nlogn)


Import heapq


另一种解决方案就是heapq,它是Python的一个标准库。heapq实现了一种叫做堆的数据结构,是一种简洁的二叉树。他能确保父节点总是比子节点小,

heappush(self.event_queue, (first_record_time, "Monitor")) (first_record_time, "Monitor") push到self.event_queue (是一个列表中)中

heappop(self.event_queue, (first_record_time, "Monitor"))



4.2 Queue的使用

队列,可以实现fifo,lifo,或者其他的优先级输出。


self.user_queue = queue.Queue()

self.user_queue.get()

self.user_queue.put()


4.3


4.3.1类属性和类实例属性

class Student(object):

count = 0

books = []  #类属性,所以的实例 共有的

def __init__(self, name, age):

self.name = name

self.age = age #类实例属性,不同的实例各自拥有

 

4.3.2 类方法和实例方法

实例方法 def hahaself): 参数是self

类方法 def weicls) : 参数是cls


4.3.3 类的相互作用

在一个类中,可以生成另一个类的实例。

比如在类windows里, next_user = User(next_user_arrive_time)

使用了类User,新生成实例 next_user ,传入的参数是next_user_arrive_time


4.4其他

import queue

Py3中 为小写,py2中为Queue queue.Queue


堆的这种数据结构:是一个二叉树,

Heapq模块 可以 对 堆进行操作


Pytnonnumpy里有现成的生成服从某个概率分布的随机变量,而且可以自己设置参数和随机数的数量。numpy.random.beta(a, b[, size]) Beta分布随机变量


numpy.random.binomial(n, p[, size]) 二项分布随机变量


numpy.random.chisquare(df[, size]) 卡方分布随机变量


numpy.random.dirichlet(alpha[, size]) 狄利克雷分布随机变量


numpy.random.exponential([scale, size]) 指数分布随机变量


numpy.random.geometric(p[, size]) 几何分布随机变量


numpy.random.normal([loc, scale, size]) 正态分布随机变量


numpy.random.poisson([lam, size]) 泊松分布随机变量


numpy.random.uniform([low, high, size]) 均匀分布随机变量


numpy.random.wald(mean, scale[, size]) Wald分布随机变量



4.5写代码中碰到的错误

经常见到的错误:仔细寻找错误的根源,一般是调用某个子函数时,子函数内部出的问题。导致主函数运行到这一步时,执行不下去了,就报了个错

Traceback (most recent call last):

File "/home/zhangyanhe/PycharmProjects/test/排队论.py", line 72, in <module>

mm1.start_sim()

File "/home/zhangyanhe/PycharmProjects/test/排队论.py", line 41, in start_sim

handle_event(a[1])

File "/home/zhangyanhe/PycharmProjects/test/排队论.py", line 51, in handle_event

user_queue.put(next_user)

TypeError: put() missing 1 required positional argument: ‘item‘



2018102419:47:28

NS3 第四次作业

py查找文件

-----------------------------------------------------

2018102613:37:14


写完这部分,读历史记录进行总结

混合编程问题 python cc

Waf大概就是基于python的一个类似make的程序。其实可以在python中直接调用cc程序的啊 绝对是可以的。生成动态链接库 .so文件,到python中调用。但是这个方法对c很成立。在c++中有各种问题。很烦人. 不要读中文博客,很多傻逼写的东西。


问题: gcc找不到头文件 :在最后 用-Ii L选项 把目录加上编译:

先实践,然后写写原理:


vim查找:normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车。 Vim会跳转到第一个匹配。按下n查找下一个,按下N查找上一个。




走了很远 了 2018102621:34:31

1.使用swig 实现c++链接到python中的操作。http://cering.github.io/2015/12/08/%E4%BD%BF%E7%94%A8SWIG%E5%AE%9E%E7%8E%B0Python%E8%B0%83%E7%94%A8C-C-%E4%BB%A3%E7%A0%81/

碰到问题:gcc找不到python.h ,设置环境变量,增加gcc的搜索路径:$C_INCLUDE_PATH=/opt/example/include

$export C_INCLUDE_PATH


$CPLUS_INCLUDE_PATH=/opt/example/include

$export CPLUS_INCLUDE_PATH


$LIBRARY_PATH=/opt/example/lib

$export LIBRARY_PATH


还是没搞定:很多不懂的啊。底层的东西都不明白。慢慢来。放弃此题。


2018102712:17:02 py练习题2 数据库 mysql

https://blog.csdn.net/San_South/article/details/80715682

linuxpipUbuntu16.04pip报错ModuleNotFoundError: No module named ‘pip._internal‘



python---10月

标签:这一   实现   子节点   lang   monitor   mat   比较   gcc   ubunt   

原文地址:https://www.cnblogs.com/zyhe/p/9918222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!