libra-gyf

Github base demo 01-02 集合与列表的小demo

"""  01  有如下值集合 [11,22,33,44,55,66,77,88,99,90],将所有大于
66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个
key的值中。即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}"""

def demo_1(n):
    k1 = []
    k2 = []
    dic ={'k1':k1,'k2':k2}
    for i in n:
        if i >66:
            k1.append(i)
        else:
            k2.append(i)
    return dic
print(demo_1([11,22,33,44,55,66,77,88,99,90]))
""" 02   查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。"""
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", "aric", "Alex", "Tony", "rain")

def demo_1(n):
    x = []
    y = []
    ret = {'移除每个元素的空格后': x, '以a或A开头并且以c结尾的元素': y}
    for i in n:
        a = i.strip()
        x.append(a)
        if (a.find('a') == 0 or a.find('A') == 0) and a.endswith('c') == True:
            y.append(a)
    return ret
print(demo_1(li))

#------------------------------------------------------------------------

dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}

def demo_2(n):
    x = []
    y = []
    ret = {'移除每个元素的空格后': x, '以a或A开头并且以c结尾的元素': y}
    for i in n.values():
        a = i.strip()
        x.append(a)
        if (a.find('a') == 0 or a.find('A') == 0) and a.endswith('c') == True:
            y.append(a)
    return ret
print(demo_2(dic))

posted on 2020-01-10 14:35  libra-gyf  阅读(121)  评论(0编辑  收藏  举报

导航