标签:count 字母 min 指定 split pytho art split() sort
复制列表>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a = l[:]
>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
列表按元素遍历的坑
按元素遍历的同时不能删除元素,删除会有问题,可以按长度遍历
>> for v in a:
... if i == 1:
... a.remove(i)
...
>> a
[1, 1, 1, 2]
删除列表中的重复元素,不保留元素
计算列表中某个重复元素的个数,遍历个数一次删除,
>> l
[1, 3, 4, 2]
>> for v in l:
... for n in range(l.count(v)):
... l.remove(v)
...
>> l
[3, 2]
#remove删除完再删除会报错,可利用try-except
>> l = [1,1,1,2,3,4,2]
>> while 1:
... try:
... l.remove(1)
... except:
... break
...
>> l
[2, 3, 4, 2]
删除列表中的重复元素,保留一个元素>> for v in l:
... for n in range(l.count(v)-1):
... l.remove(v)
...
>> l
[1, 3, 4, 2]>>?a=?[1,1,1,1,1,2,3]
>>?while?1:
...?????try:
...?????????a.remove(1)
...?????except:
...?????????break
...
>>?a=?[1,1,1,1,1,2,3]
>>?while?1:
...?????try:
...?????????a.remove(1)
...?????except:
...?????????break
...
习题9:[1,-1,2,-2,3,-3] 利用max()排序
按长度遍历列表,每次把最大值添加到一个列表,把最大值插入到另一个列表的0位置
l = [1,-1,2,-2,3,-3]
result_asc = []
result_desc = []
for i in range(len(l)):
result_desc.append(max(l))
result_asc.insert(0,max(l))
l.remove(max(l))
print("升序:",result_asc)
print("降序:",result_desc)
利用min()函数升序
l = [1,-1,2,-2,3,-3]
for i in range(len(l)):
result_2.append(min(l))
l.remove(min(l))
print(result_2
list()工厂函数
字符串转列表
>> list("abc")
[‘a‘, ‘b‘, ‘c‘]
元组转列表
>> list((1,2,3))
[1, 2, 3]
字典转列表
只转key
>> list({1:2,3:4})
[1, 3]
集合转列表
>> list({1,2,3,4})
[1, 2, 3, 4]
习题10:a = "abcdefghi"把开头、结尾、中间位置的字母变为1其他字母不变
s = "abcdefghij"
l = list(s)
if len(l)%2 == 1:
l[0],l[-1],l[len(l)//2]= "1","1","1"
else:
l[0],l[-1],l[len(l)//2-1:len(l)//2+1]= "1","1","1"
print("".join(l))
list.pop()函数
不带参数
pop() 删除并返回列表的最后一个元素
>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> l.pop()
9
>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>> a = l.pop()
>> a
8
带索引位置
删除并返回指定索引位置的元素>> l
[0, 1, 2, 3, 4, 5, 6, 7]
>> l.pop(1)
1
>> l
[0, 2, 3, 4, 5, 6, 7]
list.index(item,[start,end])
在指定范围内返回第一个item元素的索引位置,不指定范围在整个列表查找
>> a
[1, 2, 3, 2]
>> a.index(2)
1
>> a.index(2)
1>> a.index(2,0,5)
1>> a.count(3)
1
>> a.count(2)
2
List1.extend(seq)
把序列seq的每个元素追加到list1
>> l
[0, 2, 3, 4, 5, 6, 7]
>> l.extend("abc")>> l.extend([1,2,3])
>> l.extend(("x","y"))
>> l
[0, 2, 3, 4, 5, 6, 7, ‘a‘, ‘b‘, ‘c‘, 1, 2, 3, ‘x‘, ‘y‘]
list.reverse()只是翻转字符串
[3, 2, 1, 4]
>> l.reverse()
>> l
[4, 1, 2, 3]
list.sort(key=func,reverse=False)
key指定排序规则的函数
reverse True 降序,False 升序
排序过程:
list中的每个元素传入函数func,按照fanc的返回结果进行排序
按元素长度排序
>> l = [(1,2),(2,),(3,4,5,6)]
>> l.sort(key=lambda x:len(x))
>> l
[(2,), (1, 2), (3, 4, 5, 6)]
按照第一个元素排序
>> a = [(-1,),(1,1),(-2,2,3),(2,2)]
>> a.sort(key=lambda x:x[0])
>> a
[(-2, 2, 3), (-1,), (1, 1), (2, 2)]>> def self_len(x):
... return x[0]
...
>> a.sort(key=self_len)
>> a
[(-2, 2, 3), (-1,), (1, 1), (2, 2)]
按元素和排序
>> a
[(-2, 2, 3), (-1,), (1, 1), (2, 2)]
>> a.sort(key=lambda x:sum(x))
>> a
[(-1,), (1, 1), (-2, 2, 3), (2, 2)]
习题11:找到英文句子中最长的单词
方式1:
利用sort函数,按照单词长度排序
s = "I am a boyboy! hi,glory road"
for v in s:
if not v.isalpha():
s = s.replace(v," ")
l = s.split()
l.sort(key=len,reverse=True)
#l.sort(key=lambda x:len(x))
print(l[0])
s = "I am a boyboy! hi,glory road"
for v in s:
if not v.isalpha():
s = s.replace(v," ")
l = s.split()
max_length = 0
result =[]
for v in l:
if max_length < len(v):
max_length = len(v)
for v in l:
if len(v) == max_length:
result.append(v)
print(result[0])
列表引用
修改列表的引用会影响原列表
>> a = [1,2,3]
>> b = a
>> b
[1, 2, 3]
>> b.append(4)
>> b
[1, 2, 3, 4]
>> a
[1, 2, 3, 4]
副本复制
>> b = a[:]
>> id(b)
34881736
>> id(a)
43654600
>> b.append(4)
>> a
[1, 2, 3, 4]
>> b
[1, 2, 3, 4, 4]
推导列表
单层循环
>> [x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>> [x for x in range(0,10,2)]
[0, 2, 4, 6, 8]
单层循环带if
>> [x for x in range(10) if x%2==1]
[1, 3, 5, 7, 9]
双层循环
>> [x+y for x in "ABC" for y in "XYZ"]
[‘AX‘, ‘AY‘, ‘AZ‘, ‘BX‘, ‘BY‘, ‘BZ‘, ‘CX‘, ‘CY‘, ‘CZ‘]
双层循环带if
>> [x+y for x in "ABC" for y in "XYZ" if x in "AC" and y in "XZ"]
[‘AX‘, ‘AZ‘, ‘CX‘, ‘CZ‘]>> [x*y for x in range(10) for y in range(10) if x==(y+1)]
[0, 2, 6, 12, 20, 30, 42, 56, 72]
习题12:利用推导列表生成10 - 30
>> [int(x+y) for x in "123" for y in "0123456789" if int(x+y)<=30]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]>> [int(x+y) for x in "123" for y in map(str,range(10)) if int(x+y) <=30]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]>> d = {‘x‘: ‘A‘, ‘y‘: ‘B‘, ‘z‘: ‘C‘ }
>> [k + "=" + v for k,v in d.items()]
[‘x=A‘, ‘y=B‘, ‘z=C‘]
习题13:用推导列表求所有数字key的和
>> d= {1:‘a‘,2:"b","a":3}
>> sum([k for k in d if isinstance(k,(int,float))])
3
求所有数字key\value的和
>> sum([x for x in a if isinstance(x,int)] + [y for y in a.values() if isinstance(y,int)])
17
二维矩阵的转置
a=[[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]
]
方式1:遍历二维矩阵
a=[[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]
]
result = []
for i in range(3):
temp = []
for j in range(4):
temp.append(a[j][i])
result.append(temp)
print(result)
方式2:利用推导列表
>> [ [value[i] for value in a] for i in range(3)]
[[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
for?i?in?range(3):因为有3列,所以遍历3此
for?j?in?a:遍历每一行
当i?=0时:[j[i]?for?j?in?a]?表示获取每行的第1列,放到列表中
当i?=1时:[j[i]?for?j?in?a]?表示获取每行的第2列,放到列表中
当i?=2时:[j[i]?for?j?in?a]?表示获取每行的第3列,放到列表中
删除二维矩阵的第一列
方式1:遍历删除
a=[[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]
]
result = []
for i in range(3):
for j in range(4):
if i == 0:
del(a[j][i])
print(a)
方式2:遍历把除第1列的元素加入列表
a=[[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]
]
result = []
for i in range(4):
temp = []
for j in range(1,3):
temp.append(a[i][j])
result.append(temp)
print(result)
方式3:推导列表
>> [ [value[i] for i in [1,2]] for value in a]
[[2, 3], [5, 6], [8, 9], [11, 12]]
for?i?in?a:遍历列表的每一行
for?j?in?[1,2]:遍历列表的第二列和第三列
j分别取1,2代表列表的第二列和第三列
[i[j]?for?j?in?[1,2]]:
表示每一行的第二列和第三列元素,存到子列表中
标签:count 字母 min 指定 split pytho art split() sort
原文地址:http://blog.51cto.com/13496943/2298363