重要汇总

1、自动识别,一 一对应

a,b = 1,2
print(a,b)   # 1 2
a,b = [1,2]
print(a,b)   #a=1 b=2
a,b = (1,2)
print(a,b) #a=1 b=2
a,b = [1,2],[3,4]
print(a,b)   #a=[1, 2]  b=[3, 4]

2、一行代码实现两个变量值的互换

a = 1
b = 2
a,b = b,a
print(a,b)

 3、去重

li = [1,2,33,33,2,1,4,5,6,6]
set1 = set(li)
print(set1)  #{1, 2, 33, 4, 5, 6}
li = list(set1)
print(li)   #[1, 2, 33, 4, 5, 6]

 4、浅拷贝

l1 = [1,[1,3],2,3,4]
l2 = l1[:]
# print(id(l1),id(l2))
print(l1[1] is l2[1])
print(id(l1[1]),id(l2[1]))
l1[1].append('a')
print(l1,l2)   # [1, [1, 3, 'a'], 2, 3, 4] [1, [1, 3, 'a'], 2, 3, 4]
#说明这个切片也是浅拷贝

 5.如果元组里面只有一个元素,且不加逗号,那么该元素是什么类型,它就是什么类型

tu = (1)   数字
tu2 = (1,)   元组
tu = ([1])   列表
tu = ([1],)   元组

 6.斐波那契数列1,2,3,5,8,13,21.....根据这样的规律,编程求出400万以内最⼤的斐波那契数,并求出他是第⼏个斐波那契数

#方法一
a = 1
b = 2
count = 2
while a+b < 4000000:
    c = a+b
    count += 1
    a = b
    b = c
print(c)
print(count)

#方法二
l = [1,2]
while l[-1] < 4000000:
    sum = l[-1] + l[-2]
    if sum > 4000000:
        break
    else:
        l.append(sum)
print(l[-1])
print(len(l))

7.    dicta = {"a":1,"b":2,"c":3,"d":4,"f":"hello"}

  dictb = {"b":3,"d":5,"e":7,"m":9,"k":"world"}

    要求写⼀段代码,实现两个字典的相加,不同的key对应的值保留,相同的key对应的值相加后保 留,如果是字符串就拼接,如上⽰例得到结果为:dictc = {"a":1,"b":5,"c":3,"d":9,"e":7,"m":9,"f":"hello","k":"world"}

dicta = {'a':1,'b':2,'c':3,'d':4,'f':'hello'}
dictb = {"b":3,"d":5,"e":7,"m":9,"k":"world"}
ret = set(dicta.keys())|set(dictb.keys())
dic = {}.fromkeys(ret,None)
for k in dic:
    if k in dicta and k in dictb:
        dic[k] = dicta[k] + dictb[k]
    elif k in dicta:
        dic[k] = dicta[k]
    else:
        dic[k] = dictb[k]
print(dic)
View Code

8. 请写出log的实现(主要功能是打印函数名)

# @log def now():
# print ‘2017-08-31’
# >>>now()
# 输出:
# call now():
# 2017-08-31
def log(func):
    def inner(*args,**kwargs):
        print('call %s():'%func.__name__)
        ret = func(*args,**kwargs)
        return ret
    return inner
View Code

9.  有⼀个数据结构如下所⽰,请编写⼀个函数从该结构数据中返回由指定的字段和对应的值组成的字 典。如果指定字段不存在,则跳过该字段。

# data:{"time":"2016-08-05T13:13:05",
# "some_id":"ID1234",
# "grp1":{ "fld1":1,"fld2":2},
# "xxx2":{ "fld3":0, "fld5":0.4},
# "fld6":11,
# "fld7":7,
# "fld46":8}
# fields:由"|"连接的以"fld"开头的字符串,如:fld2|fld3|fld7|fld19

#方法一:
data = {"time":"2016-08-05T13:13:05",
      "some_id":"ID1234",
      "grp1":{ "fld1":1,
               "fld2":2},
      "xxx2":{ "fld3":0,
               "fld5":0.4},
      "fld6":11,
      "fld7":7,
      "fld46":8}
fields = "fld2|fld3|fld7|fld19"
def select(data,fields):
    result = {}
    field_lst = fields.split("|")
    for k in data:
        if k in fields:
            result[k] = data[k]
        elif type(data[k]) == dict:
            for j in data[k]:
                if j in fields:
                    result[j] = data[k][j]
    return result
print(select(data,fields))

#方法二:堆栈思想
data = {"time":"2016-08-05T13:13:05",
      "some_id":"ID1234",
      "grp1":{ "fld1":1,
               "fld2":2,
               'demo':{"fld19" : 666}},
      "xxx2":{ "fld3":0,
               "fld5":0.4},
      "fld6":11,
      "fld7":7,
      "fld46":8}
fields = "fld2|fld3|fld7|fld19"
def select(data,fields):
    result = {}
    field_lst = fields.split('|')
    l = [data]
    while l:
        data = l.pop()    #栈  l = []   data = { "fld3":0,"fld5":0.4,}  data = { "fld1":1,"fld2":2}
        for k in data:
            if type(data[k]) is dict:
                l.append(data[k])
            elif k in field_lst:
                result[k] = data[k]
    return result
print(select(data,fields))
View Code

 

posted @ 2017-12-20 15:09  笨笨侠  阅读(175)  评论(0编辑  收藏  举报