美文网首页
day7 --作业

day7 --作业

作者: Dxes | 来源:发表于2019-11-11 21:03 被阅读0次

    统计某个元素在列表中出现的次数

    names = ["曹操","刘备","赵云","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
    count = 0
    value = "小乔"
    for index in range(len(names)):
        if names[index] == value:
            count += 1
    print(count)
    

    在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

    names = ["曹操","刘备","赵云","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
    value = list(input("请输入一个序列:"))
    for index in range(len(value)):
        names.append(value[index])
    print(names)
    # print(names + value)
    

    从列表中找出某个值第一个匹配项的索引位置

    names = ["曹操","刘备","赵云","小乔",("貂蝉","吕布"),"诸葛亮","司马懿","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
    value = ("貂蝉","吕布")
    for index in range(len(names)):
        if names[index] == value:
            print(index)
            break
    

    反向列表中元素

    names = ["曹操","刘备","赵云","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
    for index in range(len(names)):
        for index1 in range(len(names)):
            if index > index1:
                names[index],names[index1] = names[index1],names[index]
                # print(names)
    print(names)
    

    相关文章

      网友评论

          本文标题:day7 --作业

          本文链接:https://www.haomeiwen.com/subject/dkjxictx.html