列表的练习

列表的练习

# 1. 使用切片获取列表的中间元素(不包括收尾元素)
n1 = [11, 22, 33, 44, 55, 66]
print(n1[1: (len(n1)-1)])

# 参考答案一
print(n1[1:5])
# 参考答案二
print(n1[1:-1])

# 2.使用切片将下面列表倒序
n2 = [1, 22, 13, 24, 5, 86]
print(n2[::-1])

# 参考答案一样

# 3.遍历下面列表,打印所有负数
n3 = [1, -22, 13, -24, -5, 86]
for n in n3:
    if n < 0:
        print(n)

# 参考答案一样

# 4.遍历列表,打印所有负数和对应的索引
n4 = [1, -22, 13, -24, -5, 86]

for i, n in enumerate(n4):
    if n < 0:
        print(i, n)

# 参考答案一样

# 5. 遍历n6列表,并将所有偶数加入到n5中
# 方式1
n5 = [2, 4, 6]
n6 = [3, -3, 8, 1, 10, -6, 12]
for n in n6:
    if n % 2 == 0:
        n5.append(n)
print(n5)
# 方式2
n5 = [2, 4, 6]
n6 = [3, -3, 8, 1, 10, -6, 12]
for i in range(len(n6)):
    if n6[i] % 2 == 0:
        n5 += n6[i:i+1]
print(n5)

# 参考答案
n5 = [2, 4, 6]
n6 = [3, -3, 8, 1, 10, -6, 12]
for n in n6:
    if n % 2 == 0:
        n5 += [n]       # 等价于 n5 = n5 + [n]
print(n5)

''' 基础题 '''

# 1.自定义一个数字列表,获取这个列表中的最小值
ages = [1, -3, 2, 5, -4, 99, 7, 8, 5, 3]
# 方式1  最简单的方式
# print(min(ages))

# 方式2 for遍历
min1 = ages[0]
for n in ages:
    if n < min1:
        min1 = n
print(min1)

# 2. 自定义一个数字列表,元素为10个, 找出列表中最大数连同下标一起输出
# ages = [1, -3, 2, 5, -4, 99, 7, 8, 5, 3]
# print(max(ages), ages.index(max(ages)))

# for 遍历
# max1 = ages[0]
# for n in ages:
#     if n > max1:
#         max1 = n
# print(max1)
# 但是没有得到下标

# 参考答案一
ages = [1, -3, 2, 99, -4, 99, 7, 8, 5, 3]
max1 = ages[0]
index1 = 0
for i, n in enumerate(ages):
    while n > max1:
        max1 = n
        index1 = i
print(max1, index1)  # 得到的只有第一个最大值和第一个最大值的下标

# 想得到全部的最大值及全部最大值的下标可以再继续循环
ages = [1, -3, 2, 99, -4, 99, 7, 8, 5, 3]
max1 = ages[0]
index1 = 0
for n in ages:
    if n > max1:
        max1 = n
for j, m in enumerate(ages):
    if m == max1:
        print(j, m)

# 3. 自定义一个数字列表,求列表中第二大数的下标
ages = [1, -3, 2, 5, -4, 99, 7, 8, 5, 3]
# ages.pop(ages.index(max(ages)))
# print(max(ages), ages.index(max(ages)))
# 当存在两个一样大的数时是不成立的

# 参考答案
max1 = max(ages)

index1 = 0
max2 = ages[0]
for i, n in enumerate(ages):

    if n > max2 and n != max1:
        max2 = n
        index1 = i

print(index1, max2)

# 参考答案二
max1 = max(ages)

index1 = 0
max2 = ages[0]
for i, n in enumerate(ages):
    if n == max1:
        continue

    if n > max2:
        max2 = n
        index1 = i

print(index1, max2)

# 若是同时有多个第二大的数:
ages = [1, -3, 2, 5, -4, 99, 7, 8, 8, 5, 3]

max1 = max(ages)

max2 = ages[0]
for n in ages:

    if n == max1:
        continue

    if n > max2:
        max2 = n

for j, m in enumerate(ages):
    if m == max2:
        print(j, m)
 

# 1. 使用切片获取列表的中间元素(不包括收尾元素)
n1 = [11, 22, 33, 44, 55, 66]
print(n1[1: (len(n1)-1)])

# 参考答案一
print(n1[1:5])
# 参考答案二
print(n1[1:-1])

# 2.使用切片将下面列表倒序
n2 = [1, 22, 13, 24, 5, 86]
print(n2[::-1])

# 参考答案一样

# 3.遍历下面列表,打印所有负数
n3 = [1, -22, 13, -24, -5, 86]
for n in n3:
    if n < 0:
        print(n)

# 参考答案一样

# 4.遍历列表,打印所有负数和对应的索引
n4 = [1, -22, 13, -24, -5, 86]

for i, n in enumerate(n4):
    if n < 0:
        print(i, n)

# 参考答案一样

# 5. 遍历n6列表,并将所有偶数加入到n5中
# 方式1
n5 = [2, 4, 6]
n6 = [3, -3, 8, 1, 10, -6, 12]
for n in n6:
    if n % 2 == 0:
        n5.append(n)
print(n5)
# 方式2
n5 = [2, 4, 6]
n6 = [3, -3, 8, 1, 10, -6, 12]
for i in range(len(n6)):
    if n6[i] % 2 == 0:
        n5 += n6[i:i+1]
print(n5)

# 参考答案
n5 = [2, 4, 6]
n6 = [3, -3, 8, 1, 10, -6, 12]
for n in n6:
    if n % 2 == 0:
        n5 += [n]       # 等价于 n5 = n5 + [n]
print(n5)

''' 基础题 '''

# 1.自定义一个数字列表,获取这个列表中的最小值
ages = [1, -3, 2, 5, -4, 99, 7, 8, 5, 3]
# 方式1  最简单的方式
# print(min(ages))

# 方式2 for遍历
min1 = ages[0]
for n in ages:
    if n < min1:
        min1 = n
print(min1)

# 2. 自定义一个数字列表,元素为10个, 找出列表中最大数连同下标一起输出
# ages = [1, -3, 2, 5, -4, 99, 7, 8, 5, 3]
# print(max(ages), ages.index(max(ages)))

# for 遍历
# max1 = ages[0]
# for n in ages:
#     if n > max1:
#         max1 = n
# print(max1)
# 但是没有得到下标

# 参考答案一
ages = [1, -3, 2, 99, -4, 99, 7, 8, 5, 3]
max1 = ages[0]
index1 = 0
for i, n in enumerate(ages):
    while n > max1:
        max1 = n
        index1 = i
print(max1, index1)  # 得到的只有第一个最大值和第一个最大值的下标

# 想得到全部的最大值及全部最大值的下标可以再继续循环
ages = [1, -3, 2, 99, -4, 99, 7, 8, 5, 3]
max1 = ages[0]
index1 = 0
for n in ages:
    if n > max1:
        max1 = n
for j, m in enumerate(ages):
    if m == max1:
        print(j, m)

# 3. 自定义一个数字列表,求列表中第二大数的下标
ages = [1, -3, 2, 5, -4, 99, 7, 8, 5, 3]
# ages.pop(ages.index(max(ages)))
# print(max(ages), ages.index(max(ages)))
# 当存在两个一样大的数时是不成立的

# 参考答案
max1 = max(ages)

index1 = 0
max2 = ages[0]
for i, n in enumerate(ages):

    if n > max2 and n != max1:
        max2 = n
        index1 = i

print(index1, max2)

# 参考答案二
max1 = max(ages)

index1 = 0
max2 = ages[0]
for i, n in enumerate(ages):
    if n == max1:
        continue

    if n > max2:
        max2 = n
        index1 = i

print(index1, max2)

# 若是同时有多个第二大的数:
ages = [1, -3, 2, 5, -4, 99, 7, 8, 8, 5, 3]

max1 = max(ages)

max2 = ages[0]
for n in ages:

    if n == max1:
        continue

    if n > max2:
        max2 = n

for j, m in enumerate(ages):
    if m == max2:
        print(j, m)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值