? ? ? ?列表1 + 列表2 = 将两个列表合并成一个新的列表
list = [10,20,30]
list2 = [340,50,60]
print(list + list2)
'''
[10, 20, 30, 340, 50, 60]
'''
list = [10,20,30]
result2 = list * 3
print(result2)
'''
[10, 20, 30, 10, 20, 30, 10, 20, 30]
'''
print(list1 == [10,20,30])
list = [10,20,30]
print(10 in list) #True
print([10,20] in list) #False
list = [10,20,30]
print(sum(list))
list = [10,20,30]
print(max(list))
list = [10,20,30]
print(min(list))
list = [10,44,60,20,30]
new= sorted(list)
print(new)
#[10, 20, 30, 44, 60]
list = [10,44,60,20,30]
new= sorted(list, reverse= True)
print(new)
#[60, 44, 30, 20, 10]
list = ['abc', 2, 'hello',[1,2,3]]
print(len(list))
# 4
print(list('abc')) #['a', 'b', 'c']
print(list(range(6))) #[0, 1, 2, 3, 4, 5]
list = [10,44,60,20,30]
list.clear()
print(list)
#[]
list = [10,44,60,20,30]
list1 = list.copy()
print(list1)
list = [10,44,60,20,30]
n = list[:]
i = list + []
k = list * 1
print(n)
print(i)
print(k)
#[10, 44, 60, 20, 30]
#[10, 44, 60, 20, 30]
#[10, 44, 60, 20, 30]
list = [10,44,60,20,30,10,10,20]
print(list.count(10))
# 3
my_list = [10, 44, 60, 20, 30, 10, 10, 20]
my_list.append('abc')
print(my_list) #[10, 44, 60, 20, 30, 10, 10, 20, 'abc']
my_list.extend('abc')
print(my_list) #[10, 44, 60, 20, 30, 10, 10, 20, 'abc', 'a', 'b', 'c']
my_list = [10, 44, 60, 20, 30, 10, 10, 20]
print(my_list.index(10)) # 0
my_list = [10, 44, 60, 20, 30, 10, 10, 20]
my_list.reverse()
print(my_list)
#[20, 10, 10, 30, 20, 60, 44, 10]
my_list = [10, 44, 60, 20, 30, 10, 10, 20]
my_list.sort(reverse= True)
print(my_list)
#[60, 44, 30, 20, 20, 10, 10, 10]
[表达式 for 变量 in 容器]
result = [ 10 for i in range(5)]
print(result)
#[10, 10, 10, 10, 10]
result = [ i*8 for i in range(1,10)]
print(result)
#[8, 16, 24, 32, 40, 48, 56, 64, 72]
利用列表推导式获取nums中所有元素的个位数
nums = [19, 344, 52, 18, 77, 65, 66, 76]
# [9, 4, 2, 8, 7, 5, 6, 6]
nums = [19,344,52,18,77,65,66,76]
result = [(i%10) for i in nums]
print(result)
利用列表推导式将nums是中所有的元素都乘以2
nums = [10, 2, 4, 5, 20]
# [20, 4, 8, 10, 40]
nums2 = [10,2,4,5,20]
result2 = [i *2 for i in nums2]
print(result2)
[表达式 for 变量 in 容器 if 条件语句]
? ? ? ? ? #### 条件?值1:值2? ?(条件为真 运算结果为值1, 否则为值2)
? ? ? ? 值1? ?if条件? else 值2 (判断条件是否成立,如果成立结果为值1,否则值2)
age = int(input('年龄'))
if age < 19:
print('未成年')
else:
print('成年')
# 三目推导式
age = int(input('年龄'))
n = [print('未成年') if age < 19 else print('成年')]
result2 = [x for x in range(1,10) if x % 2]
print(result2)
输出为奇数, 因为if x % 2 进行判断时,答案为True (1)才运行,x为奇数余数才为1,所以这是判断为奇数时才继续运行
利用列表推导式提取nums中所有的偶数
nums = [89, 78, 66, 54, 43, 77, 950]
# [78, 66, 54, 950]
nums = [89, 78, 66, 54, 43, 77, 950]
result = [ i for i in nums if i % 2 == 0]
print(result)
利用列表推导式删除scores中所有的不及格分数
scores = [99, 80, 56, 73, 59, 92, 84]
scores = [99, 80, 56, 73, 59, 92, 84]
result2=[scores.remove(i) for i in scores if i < 60]
print(scores)
利用列表推导式将nums中所有的偶数都除以2,所有奇数*2
nums = [10, 3, 17, 80, 44, 5]
# [5, 6, 34, 40, 22, 10]
nums = [10, 3, 17, 80, 44, 5]
result3 = [ i//2 if i % 2 ==0 else i*2 for i in nums ]
print(result3)
分析:用的三目运算符, 遍历nums, 如果为偶数,整除2,否则*2.