习题143、生成一个新列表,-1的左边都是小于它的,右边都是大于它的。a= [-1,2,3,-3,0,-5,5]
a = [-1,2,3,-3,0,-5,5]
base = a[0]
list1=[]
list2=[]
for num in a:
if num <base:
list1.append(num)
else:
list2.append(num)
print(list1 + [base] + list2)
习题144、在列表[100, 2, 3, -3, 0, -5, 5]中实现每个元素+1,需要在原列表实现
a=[100,2,3,-3,0,-5,5]
for i in range(len(a)):
a[i]+=1
print(a)
>>> a=[100,2,3,-3,0,-5,5]
>>> [x+1 for x in a]
[101, 3, 4, -2, 1, -4, 6]
>>>
>>> a=[100,2,3,-3,0,-5,5]
>>> list(map(lambda x:x+1,a))
[101, 3, 4, -2, 1, -4, 6]
习题145、在列表中插入一个新的元素[100, 2, 3, -3, 0, -5, 5]在0的前面插入一个元素10000
算法:
1 遍历,判断是否0,不是0,把元素插入新列表
2 是0,则把带插入元素插入新列表,然后再插入0
方法1:
a=[100,2,3,-3,0,-5,5]
result=[]
target_num=10000
for i in a:
if i == 0:
result.append(target_num)
result.append(i)
else:
result.append(i)
print(result)
算法:按索引遍历列表,找到0的位置,插入1000,结束遍历
方法2:
a=[100,2,3,-3,0,-5,5]
for i in range(len(a)):
if a[i]==0:
a.insert(i,10000)
break
print(a)
习题145、列表中的第三个位置插入10000
算法:使用切片把第三个位置前的取到使用切片把位置三后的取到把10000存到一个列表,三个列表相加
a=[100,2,3,-3,0,-5,5]
result=[]
target_num=10000
result=a[:2]+[target_num]+a[2:]
print(result)
习题146、[1,-1,2,-2,3,-3] 利用max()排序
按长度遍历列表,每次把最大值添加到一个列表,把最大值插入到另一个列表的0位置
a=[1,-1,2,-2,3,-3]
result_asc=[]
result_desc=[]
for i in range(len(a)):
result_desc.append(max(a))
result_asc.insert(0,max(a))
a.remove(max(a))
print("升序:",result_asc)
print("降序:",result_desc)
利用min()函数升序
a=[1,-1,2,-2,3,-3]
result=[]
for i in range(len(a)):
result.append(min(a))
a.remove(min(a))
print(result)
习题147、a = "abcdefghi"把开头、结尾、中间位置的字母变为1其他字母不变
s="abcdefghi"
a=list(s)
if len(a)%2==1:
a[0],a[-1],a[len(a)//2]="1","1","1"
else:
a[0],a[-1],a[len(a)//2-1:len(a)//2+1]="1","1","1"
print("".join(a))
习题148、找到英文句子中最长的单词
方法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])
习题149、利用推导列表生成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]
习题150、用推导列表求所有数字key的和
>>> d={1:"a",2:"b","a":3}
>>> sum([k for k in d if isinstance(k,(int,float))])
3
习题151、"abcdefgh"里面挑出3个字母进行组合,一共有多少种组合,要求三个字母中不能有任何重复的字母,三个字母的同时出现次数,在所有组合中只能出现一次,例如出现abc了,不能出现cab和bca等
s = "abcdefgh"
result = []
temp_list = []
for k in s:
for v in s:
for j in s:
if k != v and k != j and v !=j:
if sorted(list(k+v+j)) not in [sorted(s) for s in result]:
result.append(k+v+j)
#print(result)
print("组合数:",len(result))
习题152、复制一个列表,不能使用切片和复制的函数进行赋值,尽可能使用少的内置函数
方法1:
lst=["a","b","c","d","e"]
length=0
i=0
for v in lst:
length+=1
result=[None]*length
while i<length:
result[i]=lst[i]
i+=1
print("复制后的列表为:",result)
方法2:
a=["a","b","c","d","e"]
arr_length=0
for i in a:
arr_length+=1
def iter_arr(n):
arr=[]
i=0
while i<=n-1:
arr+=[i]
i+=1
return arr
result=[""]*arr_length
for i in iter_arr(arr_length):
result[i]=a[i]
print(result)
习题153、d={-1:100,-2:200,0:300,-3:200}按照key的大小顺序升序进行输出,输出key=value -3=200,-2=200,-1=100,0=300
d={-1:100,-2:200,0:300,-3:200}
for k in sorted(d.keys()):
print("%s=%s"%(k,d[k]),end=",")
d={-1:100,-2:200,0:300,-3:200}
for k,v in sorted(d.items(),key=lambda x:x[0]):
print(str(k)+"="+str(v),end="")
习题154、一个字符串排序,排序规则:小写<大写<奇数<偶数,
原理:先比较元组的第一个值,FALSE<TRUE,如果相等就比较元组的下一个值,以此类推。
s="9a13C85c7B24A6b"
s=sorted(list(s))
lst=sorted(s,key=lambda x:(x.isdigit(),x.isdigit() and int(x)%2==0,x.isalpha() and
x.isupper(),x.isalpha() and x.islower()))
print("".join(lst))
习题155、生成一个字典,key 是26个小写字母a-z,value是她的acsii码
d={}
for k in range(97,123):
d[chr(k)]=k
print(d)
>>> {chr(i):i for i in range(97,123)}
习题156、从键盘输入两个数,并比较其大小,直到输入e/E退出程序
while 1:
first=input("请输入第一个数或e退出程序:")
if first.lower()=="e":
break
second = input("请输入第二个数或e退出程序:")
if second.lower()=="e":
break
if first>second:
print("%s 大"%first)
elif first<second:
print("%s 大"%second)
else:
print("等于")
习题157、将列表元素交替地作为建和值来创建字典
lst=list(range(10))
d={}
for i in range(0,len(lst),2):
d[lst[i]]=lst[i+1]
print(d)
习题158、分别输出字符串中奇数坐标和偶数坐标的字符
s="abcdefghijkl"
for i in range(0,len(s),2):
print("偶数坐标字符:",s[i])
print("奇数坐标字符:",s[i+1])
习题159、将一个字典的 key和value 互换
dit=dict(name="wwqq",sex="M")
dit1=dict([("a",1),("b",2),("c",3)])
dit2=dict(zip([1,2,3],[3,4,5]))
print(dit,dit1,dit2)
result={}
for k,v in dit1.items():
result[v]=k
print("key和value交换后:",result)