• python基础知识第七篇(练习)


    # a.     获取内容相同的元素列表
     l1   =  [11,22,33]
     l2   =  [22,33,44]
     for l in l1:
         if l in l2:
             print(l)

    # b. 获取 l1 中有, l2 中没有的元素列表
     l1   =  [11,22,33]
     l2   =  [22,33,44]
     for l in l1:
         if l not in l2:
             print(l)

    # c. 获取 l2 中有, l1 中没有的元素列表
     l1   =  [11,22,33]
     l2   =  [22,33,44]
     for i in l2:
         if i not in l1:
             print(i)

    # d. 获取 l1 和 l2 中内容都不同的元素
     l1   =  [11,22,33]
     l2   =  [22,33,44]
     for i in l1:
         if i not in l2:
             print(i)
     for i in l2:
         if i not in l1:
              print(i)


    #有12345678 八个数字,如何组成不重复的两位数,共有多少种?
     count=0
     for i in range(1, 9):
         for i1 in range(1,9):
             if i!=i1:
                 count +=1
     print(count)
     count=0
     list1=[1,3]
     for i in range(0,len(list1)):
         for i1 in range(0,len(list1)):
             if i!=i1:
                 count +=1

    #九九乘法表
     for i in range(1,10):
         for i1 in range(1,i+1):
             print(str(i)+"*"+str(i1)+"="+str(i*i1)+"	",end="")
         print(end="
    ")


    #公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,100文钱只能买100只鸡,有哪些组合?
     for x in range(1, (100 // 5+1) ):
         for y in range(1, (100 // 3+1) ):
             for z in range(1, 101):
                 if x + y + z == 100 and 5 * x + 3 * y + z / 3 == 1000:
                     print(x,y,z)

    #li=["dsk","ds","pp"],用下划线把列表的值连起来
     li=["dsk","ds","pp",12]
     for i in li:
         print(i,end="_")
     li=["dsk","ds","pp",12]
     li[3]=str(li[3])
     v="_".join(li)
     print(v)


    #获取元组tu=("dhik","xjo","xsx"),计算元组的长度并输出,获取元组的第二个元素并输出
     tu=("dhik","xjo","xsx")
     v=len(tu)
     print(v)
     
    tu=("dhik","xjo","xsx")
     v=tu[1]
     print(v)


    #nums=[1,3,5,2,8,4]
    #找出列表中两个元素相加等于9的结合
    nums=[1,3,5,2,8,4]
    cound=[]
    for i in nums:
        for j in nums:
            if i+j==9:
                cound.append((i,j))
    
    print(cound)
  • 相关阅读:
    多维DP UVA 11552 Fewest Flop
    思维/构造 HDOJ 5353 Average
    map Codeforces Round #Pi (Div. 2) C. Geometric Progression
    构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
    贪心+优先队列 HDOJ 5360 Hiking
    贪心 HDOJ 5355 Cake
    LIS UVA 10534 Wavio Sequence
    又见斐波那契~矩阵快速幂入门题
    Big Christmas Tree(poj-3013)最短路
    poj 2449 Remmarguts' Date 第k短路 (最短路变形)
  • 原文地址:https://www.cnblogs.com/wpy188/p/10122051.html
Copyright © 2020-2023  润新知