python基础

  1. 序列冒号的使用:-1

a = [0, 1, 2, 3, 4, 5]

print("[0, 1, 2, 3, 4, 5] 测试结果为:")

print("a[-1]-> %s" %a[-1]) ###取最后一个元素

print("a[:-1]-> %s" %a[:-1]) ### 除了最后一个取全部

print("a[::-1]-> %s" %a[::-1]) ### 取从后向前(相反)的元素

print("a[2::-1]-> %s" %a[2::-1]) ### 取从下标为2的元素翻转读取

print("a[3::-1]-> %s" %a[3::-1]) ### 取从下标为3的元素翻转读取

输出结果================================

[0, 1, 2, 3, 4, 5] 测试结果为:

a[-1]-> 5

a[:-1]-> [0, 1, 2, 3, 4]

a[::-1]-> [5, 4, 3, 2, 1, 0]

a[2::-1]-> [2, 1, 0]

a[3::-1]-> [3, 2, 1, 0]

  1. 字符串拼接数字输出,占位符号

re = math.floor(32.9)

print("floor(32.9) is ")

print("floor(32.9) is %s" %re)

print("floor(32.9) is %d" %re)

输出结果=====================================

floor(32.9) is

floor(32.9) is 32

floor(32.9) is 32

  1. 单双引号及转义字符

print("hello")

print('"hello",he said')

print("\"Hello, world!\" she said")

print("hello "+ "world")

#"""这种情况无需转义字符"""

print(""""hello!",he said to her,and 'nice to meet you!',she replyed!""")

#原始字符串,转移字符串在这里面失效

print(r'C:\Program Files\python')

输出结果==========================================

hello

"hello",he said

"Hello, world!" she said

hello world

"hello!",he said to her,and 'nice to meet you!',she replyed!

C:\Program Files\python

  1. 列表 序列

greeting = 'hello'

print("0位置是字母\"%s\" -1位置是字母\"%s\"" %(greeting[0], greeting[-1]))

#切片----------------------------------------------------------------------------

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

print("#numbers[0:1] is ")

print(numbers[0:1])

print("#numbers[0:2] is ")

print(numbers[0:2])

print("#numbers[-3:-1] is ")

print(numbers[-3:-1])

输出结果=============================================

0位置是字母"h" -1位置是字母"o"

#numbers[0:1] is

[1]

#numbers[0:2] is

[1, 2]

#numbers[-3:-1] is

[8, 9]

#序列相加---------------------------------------------------------------

print("#[1, 2, 3] + [4, 5, 6] is ")

print( [1, 2, 3] + [4, 5, 6])

#in 运算符

permissions = 'we are'

print("""#'w' in permissions""")

print('w' in permissions)

print('we' in permissions)

输出结果======================================

#[1, 2, 3] + [4, 5, 6] is

[1, 2, 3, 4, 5, 6]

#'w' in permissions

True

True

#切片插入----------------------------------------------------------------

numbers = [1,5]

numbers[1:1] = [2,3,4]

print("""#numbers 插入后是""")

print(numbers)

输出结果==============================

#numbers 插入后是

[1, 2, 3, 4, 5]

  1. 字符串

#参数--------------------------------------------------------------------------

format = "Hello, %s ,%s end"

values = ("ni",'hao')

print(format % values)

#模板

tmpl= Template("Hello, $chineseyou ,$chinesegood end")

print(tmpl.substitute(chineseyou = "ni",chinesegood = "hao"))

输出结果===========================================

Hello, ni ,hao end

Hello, ni ,hao end

#简单替换方式-----------------------------------------------------------------------

print( "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to"))

print( "{name} is approximately {value:.2f}.".format(value=math.pi, name="π"))

输出结果==============================================

to be or not to be

π is approximately 3.14.

#字段替换顺序和名称 3 2 4 1----------------------------------------------------------

print( "{foo} {1} {bar} {0}".format(1, 2, bar=4, foo=3))

输出结果===============================================

3 2 4 1

#字符串替换,使用中的fullname----------------------------------------------------------

fullname = ["nash", "sundy"]

print("Mr {name[0]}".format(name=fullname))

print('One googol is {:,}'.format(10**100))

print( "Pi is {pi:.4f}".format(pi=math.pi))

输出结果===================================================

Mr nash

One googol is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

Pi is 3.1416

#符号填充-----------------------------------------------------------------------------------------

#所有字符一共12位 000000003.14

print( '{:012.2f}'.format(math.pi))

#多余空位的对齐方式:左\居中\右

print('{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}'.format(math.pi))

输出结果===================================================

000000003.14

3.14

3.14

3.14

6. 字典

#**字典=================================
para = {}  #字典
para1 = {}  #字典
para["faultTypeName"] = "正常"
para["faultValue"] = 1
para1["faultTypeName111"] = "正常"
para1["faultValue111"] = 1
print(para)
print({**para, **para1})
{'faultTypeName': '正常', 'faultValue': 1}
{'faultTypeName': '正常', 'faultValue': 1, 'faultTypeName111': '正常', 'faultValue111': 1}

7.shape

shape[0]就是读取矩阵第一维度的长度

import numpy as np
x=np.array([[1,2,3],[4,5,6]])
print(x.shape[0])

2 #行数是2

shape[1]的使用方法

print(x.shape[1])
3 #列数是3

.shape可以快速读取矩阵的形状

import numpy as np
x=np.array([[1,2,3],[4,5,6]])
print(x.shape)
(2, 3) #2行3列

shape[-1]

-1代表最后一个,所以shape[-1]代表最后一个维度,如在二维张量里,shape[-1]表示列数,注意,即使是一维行向量,shape[-1]表示行向量的元素总数,换言之也是列数:

我们还是举上面的例子:

print(x.shape[-1])
3

8. reshape

reshape(-1,1)中的-1代表无意义

reshape(-1,1)代表将二维数组重整为一个一列的二维数组

reshape(1,-1)代表将二维数组重整为一个一行的二维数组

reshape(-1,n)代表将二维数组重整为n列的二维数组

reshape(n,-1)代表将二维数组重整为n行的二维数组

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值