首页 技术 正文
技术 2022年11月18日
0 收藏 605 点赞 4,263 浏览 3415 个字

copy

a=[1,2,3]
b=a
id(a)
55499272
id(b)
55499272

id()就是查看内存地址,是不是同一个对象。

c=a.copy()
id(c)
57940040

可见copy()出来的不是一个对象。

a=[1,2,3]
b=a
a[1]=99
a
[1, 99, 3]
b
[1, 99, 3]

列表元素变了,其他也跟着变了。

列表里的每一个元素都是独立的个体,是有自己的内存地址。

id(a)
58246344
id(a[0])
1347775968
id(a[1])
1347779104

所以a=b,表示两个列表指向了同一些元素。同一个杯子。
a的元素变了,b的元素也跟着变。

b=a.copy()
copy就相当于新起一个杯子,把元素都复制过去。

注意:

列表里有独立,也有一样?

b=a.copy()
a
[‘one’, 2, [‘triple’, 4]]
b
[‘1’, 2, [‘triple’, 4]]
id(a),id(b)
(57026760, 56966024)
id(a[0]),id(b[0])
(59464760, 33713432)
id(a[1]),id(b[1])
(1701146112, 1701146112)
id(a[2]),id(b[2])
(59447944, 59447944)
id(a[2][0]),id(b[2][0])
(59465264, 59465264)

b=a
无copy,不独立,一改皆改

b=a.copy()
浅copy,第一层独立,第二层会跟着改变

import copy

a=[1,2,[3,4]]
b=a
c=a.copy()
d=copy.deepcopy(a)

a[0]=’one’
a[2][0]=’three’

print(a)
print(b)
print(c)
print(d)

[‘one’, 2, [‘three’, 4]]
[‘one’, 2, [‘three’, 4]]
[1, 2, [‘three’, 4]]
[1, 2, [3, 4]]

copy.deepcopy()是深拷贝,所有的层数都会跟着变

————————————————————————————————

字符串

字符串方法:
s=’Hello World’
s2=s.swapcase()
print(s2)

大写变小写,小写变大写。
按住ctrl,点击swapcase,就能看到方法的解释和类似源码
def swapcase(self): # real signature unknown; restored from __doc__
“””
S.swapcase() -> str

Return a copy of S with uppercase characters converted to lowercase
and vice versa.
“””
return “”

s1=”i’ll win CHAMPION”

s1.capitalize()
“I’ll win champion”

s1.casefold()
“i’ll win champion”

s1.center(50,’-‘)
“—————-i’ll win CHAMPION—————–“

s1.count(‘i’)
2

s1.endswith(‘n’)
False
s1.endswith(‘N’)
True

s2=’a\tb’
print(s2)
ab
s2.expandtabs(16)
‘a b’

s1.find(‘win’)
5

s3='{0} will win {1}’
s3
‘{0} will win {1}’
s3.format(‘you’,’second’)
‘you will win second’

s4='{who} will win {what}’
s4.format(who=’I’,what=’champion’)
‘I will win champion’

s1
“i’ll win CHAMPION”
s1.index(‘win’)
5
s1.index(‘win’,2,11)
5

’33ddx’.isalnum()
True
’33ddx’.isalpha()
False

‘3344’.isdecimal()
True
‘3.344’.isdecimal()
False

‘3344’.isdigit()
True
‘3.344’.isdigit()
False

用isdigit()

‘_3a’.isidentifier()
True
‘3a’.isidentifier()
False
是否一个可用的变量名

‘I am’.islower()
False
‘7j’.islower()
True

‘911.111’.isnumeric()
False
‘911’.isnumeric()
True
是否只有数字

‘dd’.isprintable()
True

‘ ‘.isspace()
True
‘ ‘.isspace()
True

‘Main road’.istitle()
False
‘Main Road’.istitle()
True

‘MAIN’.isupper()
True

‘,’.join(‘abc’)
‘a,b,c’
‘-‘.join([‘abc’,’123′,’zzz’])
‘abc-123-zzz’

‘abc’.ljust(10,’*’)
‘abc*******’
len(‘abc’.ljust(10,’*’))
10
‘abc’.rjust(10,’-‘)
‘——-abc’

s1
“i’ll win CHAMPION”
s1.lower()
“i’ll win champion”
s1.upper()
“I’LL WIN CHAMPION”

s=’\n word str \t’
s.strip()
‘word str’
s.lstrip()
‘word str \t’
s.rstrip()
‘\n word str’

做转换的:
str1=’abcde’
str2=’12345′
table1=str.maketrans(str1,str2)
table2=str.maketrans(str2,str1)
str1.translate(table1)
‘12345’
str2.translate(table2)
‘abcde’

s=’abc*123′
s.partition(‘*’)
(‘abc’, ‘*’, ‘123’)

s=’1122 abc cbaabc’
s.replace(‘1′,’xx’)
‘xxxx22 abc cbaabc’
s.replace(‘c’,’9′,1)
‘1122 ab9 cbaabc’

s=’hello world’
s.rfind(‘o’)
7

s.rindex(‘o’)
7
s.rindex(‘dd’)
Traceback (most recent call last):
File “<input>”, line 1, in <module>
ValueError: substring not found

s.rpartition(‘o’)
(‘hello w’, ‘o’, ‘rld’)

s
‘hello world’
s.split()
[‘hello’, ‘world’]
s.split(‘o’)
[‘hell’, ‘ w’, ‘rld’]
s.split(‘o’,1)
[‘hell’, ‘ world’]

split是把字符串变成列表

s.rsplit()
[‘hello’, ‘world’]
s.rsplit(‘o’)
[‘hell’, ‘ w’, ‘rld’]
s.rsplit(‘o’,1)
[‘hello w’, ‘rld’]

s2=’a\nbc\n123′
s2.splitlines()
[‘a’, ‘bc’, ‘123’]

s
‘hello world’
s.startswith(‘hel’)
True
s.endswith(‘llo’)
False
s.endswith(‘ld’)
True

s=’Hello World’
s.swapcase()
‘hELLO wORLD’

s=’abc uvw’
s.title()
‘Abc Uvw’

s=’101011′
s.zfill(20)
‘00000000000000101011’

isdigit
replace
find
count
index
strip
center
split
format
join

——————————————————————————————————————

t1
(1, 2, 3, [‘a’, ‘b’, ‘c’], 3, 1)
t1.count(1)
2
t1.index(2)
1
t1.index([‘a’,’b’,’c’])
3
t1[1:-2]
(2, 3, [‘a’, ‘b’, ‘c’])

元组是只读列表,有序但不可变。
但元组里的列表是可变的,因为元组存储的是列表的地址

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859