# 直接用index函数
a.index(30)
if语句中,如果有return了之后,else语句中不能有return否则就会报错; 这个点还不太懂,一会儿问问;
我下面这行代码就是else中不能再return了,否则就是直接返回‘w’
# 22. Write a Python program to find the index of an item in a specified list.
# 如果有重复咋整,这个看起来不行啊
import ipdb
a = [10,20,30,40,50,60,70,80]
b = 70
def find_index(lst,item):
a = len(lst)
for i in range(a):
if lst[i] == item:
return i
# ipdb.set_trace()
else:
'w'
2、把二维列表变成一维列表:原来列表推导式还能列表套列表
(57条消息) 列表推导式-进阶语法-CSDNPython入门技能树
def flatten(nums):
return [x for y in nums for x in y]
a = [[1,2,3],[3,4,5],[4,5,6]]
flatten(a)
这个简单啊
# 怎么把二维列表变成一维列表
a = [[1,2,3],[3,4,5],[4,5,6]]
def flat_list(lst):
a1 =[]
for item in lst:
for it in item:
a1.append(it)
return a1
flat_list(a)
突然觉得很傻,,直接sort不香香的嘛?
# Write a Python program to find the second largest number in a list.
a =[1, 2, 3, 3, 4, 5, 4, 5, 12,10]
b = [1,1]
c=[1]
def second_lar(lst):
# 先去重,然后再操作,可以减少循环的次数
lst = list(set(lst))
if len(lst)<2:
return
if len(lst)==2 and lst[0]==lst[1]:
return
for i in range(1,len(lst)):
if lst[i-1]>=lst[i] and lst[i]<max(lst):
lst[i-1] = lst[i]
# 这里我要等到for 循环完了之后然后return
return lst[i-1]
second_lar(a)
a =[1, 2, 3, 3, 4, 5, 4, 5, 12,10]
b = [1,1]
c=[1]
def second_lar(lst):
# 先去重,然后再操作,可以减少循环的次数
lst = list(set(lst))
if len(lst)<2:
return
if len(lst)==2 and lst[0]==lst[1]:
return
lst.sort()
return lst[-2]
second_lar(a)
关于none:None 是None type
a = None
b = None
for i in range(len(lst)):
if a==None:
a = lst[i]
if b==None:
None
的用处:
- 表示空值,例如:
a = None
使变量a指向None
对象,表示这个变量是空的,类似C和C++中null
。 - Python中的函数如果没有返回语句,默认返回
None
,表示没有返回值,当然也可以显式地返回None
:return None
。
找最小值就是要把初始值设置很大
# 27. Write a Python program to find the second smallest number in a list.
def second_small(lst):
# 先去重
lst = list(set(lst))
if len(lst)<2:
return 'wu'
if len(lst)==2 and lst[0]==lst[1]:
return 'wu'
a = 100000
# b = 100000
for i in lst:
if a>=i and i> min(lst):
a=i
return a
a =[1, 2, 3, 3, 4, 5, 4, 5, 12,10]
b = [1,1]
c=[1]
second_small(a)
计算频率
import collections
my_list = [10,10,10,10,20,20,20,20,40,40,50,50,30]
con = collections.Counter(my_list)
con
找两个列表的交集
color1 = "Red", "Green", "Orange", "White","White","White","White","White","White"
color2 = "Black", "Green", "White", "Pink","White","White","White","White"
print(set(color1) & set(color2))
d1 = {}
for c in color1:
d1[c] = d1.get(c, 0 ) +1
d2 = {}
for c in color2:
d2[c] = d2.get(c, 0) +1
ans = []
for k, v in d1.items():
if k in d2:
ans.extend([k] *min(v, d2[k]))
ans
extend和append的区别:
20230228继续更新:
关于set.difference()找出两个集合的不同值
49. Write a Python program to convert a list to a list of dictionaries. Go to the editor
Sample lists: ["Black", "Red", "Maroon", "Yellow"], ["#000000", "#FF0000", "#800000", "#FFFF00"]
Expected Output: [{'color_name': 'Black', 'color_code': '#000000'}, {'color_name': 'Red', 'color_code': '#FF0000'}, {'color_name': 'Maroon', 'color_code': '#800000'}, {'color_name': 'Yellow', 'color_code': '#FFFF00'}]
n1= ["Black", "Red", "Maroon", "Yellow"]
n2=["#000000", "#FF0000", "#800000", "#FFFF00"]
list1 = []
for i,j in zip(n1,n2):
dic = {}
dic['color_name'] =i
dic['color_code'] =j
list1.append(dic)
print(list1)
# [dic['color_name'] ==i, dic['color_code']==j for i in nl for j in n2]
# 正确答案
color_name = ["Black", "Red", "Maroon", "Yellow"]
color_code = ["#000000", "#FF0000", "#800000", "#FFFF00"]
print([{'color_name': f, 'color_code': c} for f, c in zip(color_name, color_code)])
# 50. Write a Python program to sort a list of nested dictionaries.
my_list = [{'key': {'subkey': 1}}, {'key': {'subkey': 10}}, {'key': {'subkey': 5}}]
print("Original List: ")
print(my_list)
my_list.sort(key=lambda e: e['key']['subkey'], reverse=True)
print("Sorted List: ")
print(my_list)
39. Write a Python program to convert a list of multiple integers into a single integer. Go to the editor
Sample list: [11, 33, 50]
Expected Output: 113350
lst = [11, 33, 50]
int(''.join(map(str,lst)))
join的对象是iterable,可以是列表,元组,字符串,字典和集合,但是里面必须全都是字符串,否则就会报错typeerror;
这个题不大会
# 51. Write a Python program to split a list every Nth element. Go to the editor # Sample list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] # Expected Output: [['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']] C = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] def list_slice(S, step): return [S[i::step] for i in range(step)] print(list_slice(C,3))