python基本使用-python基本用法

PYTHONPATH

PYTHONPATH是python moudle的搜索路径.即import xxx会从$PYTHONPATH寻找xxx.

中文编码问题

#coding=utf-8

查看导入的包的路径

import a_module

print(a_module.__file__)

map(function, iterable, ...)

参数

function -- 函数

iterable -- 一个或多个序列

返回值

Python 2.x 返回列表。

Python 3.x 返回迭代器。

>>>def square(x) : # 计算平方数

... return x ** 2

...

>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方

[1, 4, 9, 16, 25]

>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数

[1, 4, 9, 16, 25]

# 提供了两个列表,对相同位置的列表数据进行相加

>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

[3, 7, 11, 15, 19]

What is :: (double colon) in Python when subscripting sequences?

It returns every item on a position that is a multiple of 3. Since 3*0=0, it returns also the item on position 0. For instance: range(10)[::3] outputs [0, 3, 6, 9]

每隔xxx个元素做切片

transpose

比如img是h*w*c维(h是第0维,w是第1维,c是第2维)的矩阵. 在经过transpose((2,0,1))后变为c*h*w的矩阵

enumerate

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']

>>> list(enumerate(seasons))

[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

>>> list(enumerate(seasons, start=1)) # 下标从 1 开始

[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

range(start, stop[, step])

start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);

stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

>>>range(10) # 从 0 开始到 10

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> range(1, 11) # 从 1 开始到 11

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> range(0, 30, 5) # 步长为 5

[0, 5, 10, 15, 20, 25]

>>> range(0, 10, 3) # 步长为 3

[0, 3, 6, 9]

>>> range(0, -10, -1) # 负数

[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

>>> range(0)

[]

>>> range(1, 0)

[]

super(type[, object-or-type])

#!/usr/bin/python

# -*- coding: UTF-8 -*-

class A(object): # Python2.x 记得继承 object

def add(self, x):

y = x+1

print(y)

class B(A):

def add(self, x):

super(B, self).add(x)

b = B()

b.add(2) # 3

print

print("name: "%s"" % props["name"],fp) //把字符串写入文件fp

random用法

random.choice(sequence)

random.choice 选中一个

random.sample(sequence,k=) 选中多个

import random

path = "/home/train/disk/data/yulan_park_expand"

random.choices([x for x in os.listdir(path)

if os.path.isfile(os.path.join(path, x))],k=2000)

shell 工具

from shutil import copyfile

copyfile(src, dst)

参数解析

import argparse

parser = argparse.ArgumentParser()

parser.add_argument('num',type=int,help="img numbers to random")

args = parser.parse_args()

类名直接调用

之前在pytorch和keras中经常发现一个类model被直接调用,发现很有意思。于是就去看了看pytorch中nn.Module的源码,发现是定义了__call__(self)函数再去调用forward()函数。举个例子如下:

import math

class Pow(object):

def __init__(self,n=2):

self.n=n

super(Pow,self).__init__()

def forward(self,x):

return math.pow(x,self.n)

def __call__(self,x):

return self.forward(x)

l=Pow(2)

y=l(10)

print(y) #输出结果是100

l=Pow(3)

y=l(10)

print(y) #输出结果是1000

原文地址:https://www.cnblogs.com/sdu20112013/p/11216584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值