day7-列表作业(1)

day 7

总结

1. 列表的相关操作
  1. 数学运算符: +、*

    • +:合并 : 列表1 + 列表2

      list1 = [1,2,3]
      list2 = [100,200,300]
      result = list1 + list2
      print(result)
      
    • *:重复: 列表 * N

      list1 = [1,2,3]
      result = list1 * 2
      print(result)
      
  2. 比较运算符: >、<、>=、<=、==、!=

    print(list1 == [1,2,3])   #True
    
  3. in 和 not in

    print(1 not in list1)           #False
    print([10,20] in [10,20,30])    #False
    print([10,20] in [[10,20],30])  #True
    
2.相关函数
  1. sum(列表) – 求和
  2. max(列表)、min(列表) – 求最大值和最小值
  3. len(列表) – 列表的长度
  4. sorted(列表) – 从小到大排序,产生新序列
  5. list(数据) – 将其他数据转换成列表
3. 列表的相关方法
  1. 列表.clear() – 清空列表

    nums = [10,89,23,5]
    nums.clear()
    print(nums)   # []
    
  2. 列表.copy() – 复制,返回新列表

    • 直接赋的地址–>指向相同的地址
    • 用copy,直接赋的数据,指向不同的地址
    # 直接赋的是数据,指向不同的地址
    a = [10,20,30]
    # 直接赋是赋的地址,指向相同的地址
    b = a
    # 直接赋的是数据,指向不同的地址
    c = a.copy()
    a.append(100)
    print(a,b,c)
    
  3. 列表.count(元素) - 统计列表中指定元素的个数

    nums = [23,89,78,23,10,23,9,23,10]
    result = nums.count(23)
    print(result)     # 4
    
  4. 列表.extend(序列) - 将序列中所有的元素添加到列表

    list1 = [10,20,30]
    list1.extend([100,30])
    print(list1)
    
  5. 列表.index(元素) - 获取元素的下标。元素不存在报错!

    list1 = [10,20,30,10]
    print(list1.index(10))
    
    index1 = list1.index(10)
    result = list1[index1+1:].index(10) + index1 + 1
    print(result)
    
  6. 列表.reverse() - 倒序

    list1.reverse()
    print(list1)
    
  7. 列表.sory() - 从小打到排序

    list1 = [12,34,56,2,546,34]
    list1.sort()
    print(list1)
    result = sorted(list1)
    print(result)
    
3. 列表推导式
  1. 结构一:
    • [表达式 for 变量 in 序列]
  2. 结构二:
    • [表达式 for 变量 in 序列 if 条件语句]
4.元组
  1. 什么是元组
  • 元组是容器型数据类型

  • 元组是不可变的(不支持增删改)、有序的

  • 单个元素

    • t1 = (1,) 注意要加逗号
  1. 支持查
  2. 支持相关操作:+、*、比较运算、in/not in
  3. 支持相关函数:sum/max/min/len/sorted
  4. 支持部分相关方法:count/index

作业

1.创建一个列表,列表中有10个数字, 保证列表中元素的顺序,对列表进行排重,并对列表使用进行降序排序

例如:[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
		--- 去重之后 [70, 88, 91, 107, 234, 177, 282, 197]
  	---- 降序排序 [282, 234, 197, 177, 107, 91, 88, 70]
nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
nums1 = []
for i in nums:
    if i not in  nums1:
        nums1.append(i)
nums1.sort(reverse=True)
print(nums1)
  1. 利用列表推导式, 完成以下需求

a. 生成一个存放1-100中各位数为3的数据列表

结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
result = [i for i in range(1,100) if i%10== 3]
print(result)

b. 利用列表推到是将 列表中的整数提取出来

例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
nums = [True, 17, "hello", "bye", 98, 34, 21]
result = [i for i in nums if type(i) == int]
print(result)

c.利用列表推导式 存放指定列表中字符串的长度

例如: ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
str1 = ["good", "nice", "see you", "bye"]
result = [len(i) for i in str1]
print(result)

d. 利用列表推导式删除列表中整数个位数小于5的元素

例如:[24, 'abc', 99, True, 21, 38, 'hello'] --- ['abc', 99, True, 38, 'hello']

nums = [24, 'abc', 99, True, 21, 38, 'hello']
result = [i for i in nums if not type(i)== int or (type(i) == int and i%10 > 5)]
print(result)

e. 利用列表推导式获取元素是元组的列表中每个元组的最后一个元素

例如:[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]  --- [30, 'hello', 3.4, False]

nums = [(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]
result = [list(i[-1:-2:-1])[0] for i in nums]
print(result)

f.利用列表推导式将数字列表中所有的奇数乘以2,所有的偶数除以2

例如: [23, 4, 67, 88, 90, 21]  -> [46, 2, 134, 44, 45, 42]
    
nums = [23, 4, 67, 88, 90, 21]
result = [i*2 if i%2 else i//2 for i in nums]
print(result)
  1. 已知一个列表获取列表中指定元素所有的下标

    例如:[10, 20, 34, 10, 9, 78]
    10的下标:[0, 3]
    20的下标:[1]
    30的下标:[]
    
    nums = [10, 20, 34, 10, 9, 78]
    index1 = nums.index(10)
    result = nums[index1+1:].index(10) + index1 + 1
    print(nums.index(10),result)
    
  2. *已知一个数字列表,写程序判断这个列表时候是连续递增列表。

    例如:
    [1, 2, 3, 4, 5]   -> True
    [23, 45, 78, 90]  -> True
    [1, 3, 2, 4, 5]	-> False
    
    nums = [1, 2, 3, 4, 5]
    nums1 = sorted(nums)
    if nums == nums1:
        print(True)
    else:
        print(False)
    
  3. 已知两个列表,将两个列表按照下面的规律交叉合并

    A = [10, 20, 30, 40, 50]
    B = [100, 200, 300]
    结果:[10, 100, 20, 200, 30, 300, 40, 50]
    new_list = []
    if len(A)>len(B):
        A,B=B,A
    for i in range(len(A)):
        new_list.append(B[i])
        new_list.append(A[i])
    print(new_list + B[len(A):])
    
    
  4. 已知两个有序列表,将两个列表合并,合并后的新列表中元素仍然是递增列表

    A = [10, 20, 30, 40, 50]
    B = [25, 44, 60]
    结果:[10, 20, 25, 30, 40, 45, 50, 60]
    
    new_list = A + B
    new_list.sort()
    print(new_list)
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值