python---10月


7. Reverse Integerpython

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

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


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

注意: int 能够直接把 整数前面 的0 去掉。git


Python3 中比较github

Import operatorsql

operator.lt(a, b) less than数据库

operator.le(a, b)编程

operator.eq(a, b)vim

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下看状况的。


不少时候,报错在一句。当你怎么都看不出来错误的时候,能够看看上一句。说不定错误就在那里呀。


4、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'

相关文章
相关标签/搜索