def dedupe(items, key=None): seen = set() for item in items: val = item if key is None else key(item) if val not in seen: yield item seen.add(val) def deleteRepeat(lst): ''' 列表去重 :param lst: :return: ''' if not lst: return lst try: l = list(dedupe(ls…
字典的创建有两种方式,如果出现In [26]这样的赋值方式就会报错. In [17]: s['name'] = 'alex' In [18]: s['sex'] = 'male' In [19]: s Out[19]: {'name': 'alex', 'sex': 'male'} In [20]: s = {'name':'alex','sex':'male'} In [21]: s Out[21]: {'name': 'alex', 'sex': 'male'} In [22]: t = {…
# 条件判断 elif: else if 的作用 注意: : [冒号]BMI =w/(h*h) if BMI<15: print('较轻')elif BMI<25: print('正常')else BIM: print('肥胖') # 循环 for x in xs 有点类似 C#的foreachrange() 生成整数序列,range(101) 生成0~100的整数序列 while 循环 找不同一. n = 11 while n>10: n-1 print(n)…
先看代码: In [1]: a = {'name': 'wang'} In [2]: a.get('age') In [3]: a['age'] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-3-a620cb7b172a> in <module>() ----&g…