最近去出差了,巨穷,报销还要等下个月,揭不开锅了快要
index()
列表值有一个index()方法,可以传入一个值,如果该值存在于列表中,就返回它的下标。如果该值不在列表中,Python 就报ValueError。
用append()和insert()方法在列表中添加值
要在列表中添加新值,就使用append()和insert()方法。
用remove()方法从列表中删除值
给remove()方法传入一个值,它将从被调用的列表中删除
用sort()方法将列表中的值排序
数值的列表或字符串的列表,能用sort()方法排序。
这里调用的是 example.sort() 而不是sort(example)
example = [2, 5, 3.14, 1, -7]
print('example.sort()=')
print(str(example.sort()))
如果尝试返回example.sort()可见返回的是一个None类型
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 12 14:57:55 2018
@author: Lenovo
"""
example = [2, 5, 3.14, 1, -7]
print('example.sort()=',end='')
example.sort()
print(str(example))
runfile('E:/PythonLearning/sort.py', wdir='E:/PythonLearning')
example.sort()=[-7, 1, 2, 3.14, 5]
遇到了一个int转换的问题:搜索了一下发现从转换可以发现很多有意思的事情:
检查是否为float类型:
def is_float(value):
try:
float(value)
return True
except:
return False
准确命名应该是: is_convertible_to_float(value)
神奇的Python:
val is_float(val) Note
-------------------- ---------- --------------------------------
"" False Blank string
"127" True Passed string
True True Pure sweet Truth
"True" False Vile contemptible lie
False True So false it becomes true
"123.456" True Decimal
" -127 " True Spaces trimmed
"\t\n12\r\n" True whitespace ignored
"NaN" True Not a number
"NaNanananaBATMAN" False I am Batman
"-iNF" True Negative infinity
"123.E4" True Exponential notation
".1" True mantissa only
"1,234" False Commas gtfo
u'\x30' True Unicode is fine.
"NULL" False Null is not special
0x3fade True Hexadecimal
"6e7777777777777" True Shrunk to infinity
"1.797693e+308" True This is max value
"infinity" True Same as inf
"infinityandBEYOND" False Extra characters wreck it
"12.34.56" False Only one dot allowed
u'四' False Japanese '4' is not a float.
"#56" False Pound sign
"56%" False Percent of what?
"0E0" True Exponential, move dot 0 places
0**0 True 0___0 Exponentiation
"-5e-5" True Raise to a negative number
"+1e1" True Plus is OK with exponent
"+1e1^5" False Fancy exponent not interpreted
"+1e1.3" False No decimals in exponent
"-+1" False Make up your mind
"(1)" False Parenthesis is bad
You think you know what numbers are? You are not so good as you think! Not big surprise.
如果元组中只有一个值,你可以在括号内该值的后面跟上一个逗号,表明这种情况。否则,Python 将认为,你只是在一个普通括号内输入了一个值。逗号告诉Python,这是一个元组(不像其他编程语言,Python 接受列表或元组中最后表项后面跟的逗号)。在交互式环境中,输入以下的type()函数调用,看看它们的区别:
>>> type(('hello',))
<class 'tuple'>
>>> type(('hello'))
<class 'str'>
你可以用元组告诉所有读代码的人,你不打算改变这个序列的值。如果需要一个永远不会改变的值的序列,就使用元组。使用元组而不是列表的第二个好处在于,因为它们是不可变的,它们的内容不会变化,Python 可以实现一些优化,让使用元组的代码比使用列表的代码更快。
函数list()和tuple()将返回传递给它们的值的列表和元组版本。
>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
Python列表的引用
>>> spam = [0, 1, 2, 3, 4, 5]
>>> cheese = spam
>>> cheese[1] = 'Hello!'
>>> spam
[0, 'Hello!', 2, 3, 4, 5]
>>> cheese
[0, 'Hello!', 2, 3, 4, 5]
代码只改变了cheese 列表,但似乎cheese 和spam 列表同时发生了改变。
传递引用的问题:
4.7.1 传递引用
要理解参数如何传递给函数,引用就特别重要。当函数被调用时,参数的值被复制给变元。对于列表(以及字典,我将在下一章中讨论),这意味着变元得到的是引用的拷贝。要看看这导致的后果,请打开一个新的文件编辑器窗口,输入以下代码,并保存为passingReference.py:
def eggs(someParameter):
someParameter.append('Hello')
spam = [1, 2, 3]
eggs(spam)
print(spam)
[1, 2, 3, 'Hello']
尽管spam和someParameter 包含了不同的引用,但它们都指向相同的列表。这就是为什么函数内的append('Hello')方法调用在函数调用返回后,仍然会对该列表产生影响。请记住这种行为:如果忘了Python 处理列表和字典变量时采用这种方式,可能会导致令人困惑的缺陷。
书签 4.7.2 P76 Y102