1、分别打印100以内的所有偶数和奇数并存入不同的列表当中
2、请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8]
3、将字符串类似:“k:1|k3:2|k2:9” 处理成key:value或json格式,比如{“k”: “1”, “k3”: “2”}
4、把字符串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名
(前英文为大写后英文为小写) 小驼峰:作为变量命名
5、给一组无规律的数据从大到小或从小到大进行排序如:list = [2, 6, 9, 10, 18, 15, 1]
6、如有两个list:a =[‘a’,‘b’,‘c’,‘d’,‘e’]
b =[1,2,3,4,5] 将a中的元素作为key b中的元素作为value,将a,b合并为字典
7、有如下列表,统计列表中的字符串出现的次数
#a = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
8、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]
1、分别打印100以内的所有偶数和奇数并存入不同的列表当中
方法一:
a=[]
b=[]
for i in range(0,101):
if i%2==0:
a.append(i)
elif i%2!=0:
b.append(i)
print(a)
print(b)
方法二:
a=list(range(0,101,2))
print(a)
b=list(range(1,100,2))
print(b)
方法三:
list1 = []
list2 = []
a=0
while a<=100:
if a%2==0:
list1.append(a)
a=a+1
else:
list2.append(a)
a=a+1
print (list1)
print (list2)
===============================================
2、请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8] 里面重复元素?
方法一:
list = [1, 3, 6, 9, 1, 8]
a=set(list)
print(a)
方法二:
a=[]
list = [1, 3, 6, 9, 1, 8]
for i in list:
if i not in a:
a.append(i)
print(a)
方法三:
===============================================
3、将字符串类似:“k:1|k3:2|k2:9” 处理成key:value或json格式,比如{“k”: “1”, “k3”: “2”}
方法一:
a= “k:1|k3:2|k2:9”
b = a.split("|")
print(b) #[‘k:1’, ‘k3:2’, ‘k2:9’]
c = {}
for i in b:
key,value = i.split("😊
c[key]=value
print©
方法二:
str=“k:1|k3:2 |k2:9”
print(dict([m.split(’:’) for m in str.split("|")]))
方法三:
a= “k:1|k3:2|k2:9”
s=a.split("|") #[‘k:1’, ‘k3:2’, ‘k2:9’]
print(s)
c = {}
for i in s:
b=i.split("😊
#print(b)
c.setdefault(b[0],b[1])
print©
==============================================
4、把字符串user_controller转换为驼峰命名UserController大驼峰在java用作变量命名
(前英文为大写后英文为小写) 小驼峰:作为变量命名
方法一:
a=“user_controller”
b=a.split(’’)
list=[]
for i in b:
list.append(i.capitalize())
c=’’.join(list)
print©
方法二:
a=‘user_controller’
b=a.split(’’)
print(b)
print(b[0].capitalize()+b[1].capitalize())
方法三:
str=‘user_controller’
str1=str.split(’’)
for i in str1:
j=i.capitalize()
print(j,end=’’)
方法四:(不合理)
a = ‘user_controller’
a1=a.capitalize()
a2=a1.replace(‘c’,‘C’)
for i in a2:
if i==’’:
continue
print("".join(i),end=’’)
==============================================
5、给一组无规律的数据从大到小或从小到大进行排序如:list = [2, 6, 9, 10, 18, 15, 1]
方法一: sort升序
list4 = [2, 6, 9, 10, 18, 15, 1]
list4.sort()
print(list4)
方法二:sorted
list = [2, 6, 9, 10, 18, 15, 1]
print(sorted(list,reverse=True)) #降序
print(sorted(list,reverse=False))
==============================================
6、如有两个list:a =[‘a’,‘b’,‘c’,‘d’,‘e’]
b =[1,2,3,4,5] 将a中的元素作为key b中的元素作为value,将a,b合并为字典
方法一:
a =[‘a’,‘b’,‘c’,‘d’,‘e’]
b =[1,2,3,4,5]
c=dict(zip(a,b))
print©
方法二:
a =[‘a’,‘b’,‘c’,‘d’,‘e’]
b =[1,2,3,4,5]
d={}.fromkeys(a)
e=0
for i in d:
d[i]=b[e]
e+=1
print(d)
方法三:
a=[‘a’,‘b’,‘c’,‘d’,‘e’]
b=[1,2,3,4,5]
c=[]
for key,value in zip(a,b):
d=str(key)+str(value)
c.append(d)
print(dict©)
==============================================
7、有如下列表,统计列表中的字符串出现的次数
#a = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
方法一:
a = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
b={}
for i in a:
b[i]=b.get(i,a.count(i))
print(b)
方法二:
cishu = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
set = set(cishu)
print(set)
for n in set:
sum = cishu.count(n)
print(n+’: %d’% sum)
方法三:
cishu1 = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
b1={}
for i in cishu1:
b1[i]=b1.get(i,cishu1.count(i))
print(b1)
方法四:
b={}
a = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
for i in a:
b[i]=a.count(i)
print(b)
方法五
b={}
a = [‘apple’,‘banana’,‘apple’,‘tomao’,‘orange’,‘apple’,‘banana’,‘watermeton’]
q=set(a)
for d in q:
print(d,a.count(d))
==============================================
8、列表推导式求出列表所有奇数并构造新列表 a =[1,2,3,4,5,6,7,8,9,10]
方法一:
a = [1,2,3,4,5,6,7,8,9,10]
b = []
for i in a :
if i%2==1:
b.append(i)
print(b)
==============================================