python学习(第三章)

使用字符串

基本字符串操作

  • 所有标准的字符串操作(索引、分片、乘法、判断成员资格、求长度、取最大值和最小值)对字符串同样适用。但是,请记住字符串都是不变的

    >>>website = 'http://www.python.org'
    >>>website[-3: ] = 'com'
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'str' object does not support item assignment
    

字符串格式化: 精简版

  • 字符串格式化使用字符串格式化操作符即百分号%来实现
  • 在%的左侧放置一个字符串(格式化字符串),而右侧则放置希望格式化的值。一般情况下使用元组:

    >>>format = "Hello, %s. %s enough for ya?"
    >>>values = ('world', 'Hot')
    >>>print format %s values
    Hello, world. Hot enough for ya?
    

    [注: 如果使用列表或其他序列代替元组,那么序列就会被解释为一个值。只有元组和和字典可以格式化一个以上的值]

  • %s中的s表示会被格式化为字符串--如果不是字符串,则会用str将其转换为字符串。

    [注: 如果要在格式化字符串里面包含百分号,那么必须使用%%]

  • 如果要格式化实数(浮点数),可以使用f说明符类型,同时提供所需要的精度: 一个句点在加上希望保留的小数位数.

    >>>format = "Pi with three decimals: %.3f"
    >>>from math import pi
    >>>print format % pi
    Pi with three decimals: 3.142
    

模板字符串
string模板提供另外一种格式化值的方法: 模板字符串。

>>>from string import Template
>>>s = Template('$x, glorious $x!')
>>>s.substitute(x = 'slurm')
'slurm, glorious slurm'

如果替换字段是单词的一部分,那么参数名就必须用括号括起来,从而准确指明结尾

>>>from string import Template
>>>s = Template("It's ${x}tastic!")
>>>s.substitute(x = 'slurm')
"It's slurmtastic!"

可以使用$$插入美元符号

>>>from sting import Template
>>>s = Template("Make $$ selling $x!")
>>>s.substitute(x = 'slurm')
'Make $ selling slurm!'

除了关键字参数之外,还可以使用字典变量提供值/名称对

>>>s = Template('A $thing must never $action.')
>>>d = {}
>>>d['thing'] = 'gentleman'
>>>d['action'] = 'show his socks'
>>>s.substitue(d)
'A gentleman must never show his socks.'

字符串格式化: 完整版

  • 格式化操作符的有操作数可以是任何东西,如果是元组或映射类型(如字典),那么字符串格式化将会有所不同
  • 如果有操作数是元组,则其中的每一个元素会被单独格式化,每个值都需要一个对应的转换说明符

注: 如果需要转换的元组作为转换表达式的一部分存在,那么必须将它用圆括号括起来,以避免错误。

>>>'%s plus %s equals %s' % (1, 1, 2)
'1 plus 1 equals 2'
>>>'%s plus %s equals %s' % 1, 1, 2# Lacks parentheses!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

  • 基本的转换说明符包括以下部分。

    1. %字符: 标记转换说明符的开始
    2. 转换标志(可选): -表示左对齐; +表示在转换值之前要加上正负号; ""(空白字符)表示正数之前保留空格; 0表示转换值若位数不够则用0填充
    3. 最小字段宽度(可选): 转换后的字符串至少应该具有该值指定的宽度。如果是"""",则宽度会从值元组中读出
    4. 点(.)后跟精度值(可选): 如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就是表示最大字段宽度。如果是"*",那么精度将会从元组中读出
    5. 转换类型转换类型 & 含义\d, i & 带符号的十进制整数\o & 不带符号的八进制\u & 不带符号的十进制\x & 不带符号的十六进制\X & 不带符号的十六进制\e & 科学计数法表示浮点数\E & 科学计数法表示浮点数\f, F & 十进制浮点数\g & 如果指数大于-4或者小于精度值则和e相同,其他情况与f相同\G & 如果指数大于-4或者小于精度值则和E相同,其他情况与F相同\c & 单字符\r & 字符串(使用repr转换任意python对象)\s & 字符串(使用str转换任意python对象)\

简单转换

>>>'Price of eggs: $%d' % 42
'Price of eggs: $42'
>>>'Hexadecimal price of eggs: %x' % 42
'Hexadecimal price of eggs: 2a'
>>>from math import pi
>>>'Pi: %f...' % pi
'Pi: 3.141593...'
>>>'Very inexact estimate of pi: %i' % pi
'Very inexact estimate of pi: 3'
>>>'Using str: %s' % 42L
'Using str: 42'
>>>'Using repr: %r' % 42L
'Using repr: 42L'

字段宽度和精度

>>>from math import pi
>>>'%10lf' % pi # 字段宽 10
' 3.141593'
>>>'%10.2lf' % pi # 字段宽 10, 精度 2
' 3.14'
>>>'%.2lf' % pi # 精度 2
'3.14'
>>>'%.*5s' % 'Guido van Rossum' # 字符串宽度 5
'Guido'
>>>'%.*s' % (5, 'Guido van Rossum') # 字符串宽度 5
'Guido'

符号、对齐和0填充

  • 在字段宽度和精度值之前还可以放置一个"标表",该标表可以是零、减号或空格。
  • 零表示数字会用0进行填充

    >>>from math import pi
    >>>'%010.2lf' % pi
    '0000003.14'
    
  • 减号(-)用来左对齐数值:

    >>>form math import pi
    >>>'%-10.2lf' % pi
    '3.14 '
    
  • 空白(" ")意味着在正数前加空格

    >>>print ('% 5d' % 10) + '\n' + ('% 5d' % -10)
    10
    -10
    
  • 加号(+)表示不管是正数还是负数都标示出符号

    >>>print ("%+5d' % 10) + '\n' + ('%+5d' % -10)
    +10
    -10
    
  • 示例3-1


 # 使用给定的宽度打印格式化后的价格列表
width = input('Please enter width')
price_width = 10
item_width = width - price_width
header_format = '%-*s%*s'
body_format = '%-*s*s.2f'
print '=' * width
print header_format % (item_width, 'Item', 'price_width, 'Price')
print '-' * width
print body_format % (item_width, 'Apples', price_width, 0.4)
print body_format % (item_width, 'Pears', price_width, 0.5)
print '=' * width


 $./3-1.py
Please enter width: 80
================================================================================
Item Price
--------------------------------------------------------------------------------
Apples 0.40
Pears 0.50
================================================================================

字符串方法

find

  • find方法可以在一个较长的字符串查找子字符串。它返回子串所在位置的最左端索引,如果没有找到返回-1

    >>>'With a moo-moo here. and moo-moo there'.find('moo')
    7
    >>>title = "Monty Python's Flying Circus"
    >>>title.find('Monty')
    0
    >>>title.find('Python')
    6
    >>>title.find('Zirquss')
    -1
    
  • 这个方法还可以接受可选的起始点和结束点参数:

    >>>subject = '$$$ Get rich now!!! $$$'
    >>>subject.find('$$$')
    0
    >>>subject.find('$$$', 1) # 只提供起始点
    20
    >>>subject.find('!!!')
    16
    >>>subject.find('!!!', 0, 16) # 提供起始点和结束点
    -1
    

join

  • join方法是非常重要的字符串方法,它是split方法的逆方法

    >>>seq = [1, 2, 3, 4, 5]
    >>>'+'.join(seq)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: sequence item 0: expected string, int found
    >>>seq = ['1', '2', '3', '4', '5']
    >>>'+'.join(seq)
    '1+2+3+4+5'
    >>>dirs = '', 'usr', 'bin', 'env'
    >>>'/'.join(dirs)
    '/usr/bin/env'
    

    需要添加的队列元素都必须是字符串

lower

  • lower方法返回字符串的消协字母版

    >>>'Trondheim Hammer Dance'.lower()
    'trondheim hammer dance'
    

replace

  • replace方法返回某字符串的所有匹配项均被替换之后得到字符串

    >>>'This is a test'.replace('is', 'eez')
    'Theez eez a test'
    

split

  • 它是join的逆方法,用来将字符串分割成序列

    >>>'1+2+3+4+5'.split('+')
    ['1', '2', '3', '4', '5']
    >>>'/usr/bin/env'.split('/')
    ['usr', 'bin', 'env']
    
  • strip方法返回取出两侧(不包括内部)空格的字符串

    >>>' internal whitespace is kept '.strip()
    'internal whitespace is kept'
    
  • 也可以指定需要去除的字符,将它们列为参数即可

    >>>'***SPAM * for * everyone!!! ***'.strip(' *!')
    'SPAM * for * everyone'
    

translate

  • translate方法和replace方法一样,可以替换字符串中的某些部分,但是和前者不同的是,translate方法只处理单个字符。它的优势在于可以同时进行多个替换,有些时候比replace效率高得多

  • 在使用translate转换之前,需要先完成一张转换表。转换表中是以某字符替换某字符的对应关系

  • maketrans函数接受两个参数: 两个等长的字符串,表示第一个字符串中的每个字符都用第二个字符串中相同位置的字符替换。

    >>>from string import maketrans
    >>>table = maketrans('cs', 'kz')
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在《python深度学习》的第八章中,主要讨论了在ImageNet大规模视觉识别挑战(ILSVRC)上训练深度神经网络的主题。其中最后一章讨论了SqueezeNet深度学习架构[1]。SqueezeNet是在2016年的一篇论文中提出的,它在准确度上和AlexNet相当,但参数减少了50倍,模型大小小于0.5MB。 在这一章中,还提到了一个名为test_alexnet.py的脚本,用以测试在ImageNet上训练的CNN模型。这个脚本没有进行任何修改,因为该章节中的test_*.py脚本旨在成为可以应用于任何在ImageNet上训练的CNN模型的模板。 此外,这一章还介绍了处理问题的一种方法,即使用softmax温度。在使用softmax温度时,需要尝试多种不同的温度值,以找到合适的温度值。 总结起来,《python深度学习》第八章主要涵盖了SqueezeNet深度学习架构、在ImageNet上训练的CNN模型的测试脚本以及使用softmax温度来处理问题的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python视觉深度学习系列教程 第三卷 第8章 在ImageNet上训练SqueezeNet](https://blog.csdn.net/bashendixie5/article/details/122175562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [《python深度学习学习笔记与代码实现(第八章:8.1,8.2,8.3)](https://blog.csdn.net/qq_41718518/article/details/90216766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值