from collections import defaultdict
from collections import OrderedDict d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(5) print(d) #di = dict(list)
#di['b'].append(11)
#di['b'].append(22)
#di['c'].append(55)
#print(di) ds = defaultdict(set)
ds['b'].add(11)
ds['b'].add(22)
ds['c'].add(33)
print(ds) do = OrderedDict()
do['foo'] = 1
do['far'] = 2
do['coo'] = 5 for key in do:
print(key, do[key]) # 字典的运算
prices = {
'IBM': 205.5,
'APPLE': 500,
'HP': 43,
'FB': 88.3
} min_prices = min(zip(prices.values(), prices.keys()))
print(min_prices) max_prices = max(zip(prices.values(), prices.keys()))
print(max_prices) prices_sorted = sorted(zip(prices.values(), prices.keys()))
print(prices_sorted) z1 = [1,2,3]
z2 = ['A','B','C']
z3 = zip(z1, z2)
print(list(z3))
z4 = zip(*zip(z1, z2))
print(list(z4)) x = [1,2]
y = ['a','b','c']
print(list(zip(x,y))) #zip()配合*号操作符,可以将已经zip过的列表对象解压, 并且返回的是元祖 #使用zip和iterators生成滑动窗口 (n -grams)
from itertools import islice
def n_grams(a, n):
z = (islice(a, i, None) for i in range(n))
return zip(*z) a = [1,2,3,4,5,6]
print('(a,3):',list(n_grams(a,3)))
print('(a,2):',list(n_grams(a,2)))
print('(a,4):',list(n_grams(a,4))) # 使用zip反转字典
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print('m.items = ', m.items())
print(list(zip(m.values(), m.keys())))
md = dict(zip(m.values(), m.keys()))
print(md)

  

Python3之Zip的更多相关文章

  1. 用python实现矩阵转置,python3 中zip()函数

    前几天群里有同学提出了一个问题:手头现在有个列表,列表里面两个元素,比如[1, 2],之后不断的添加新的列表,往原来相应位置添加.例如添加[3, 4]使原列表扩充为[[1, 3], [2, 4]],再 ...

  2. Python3实现zip分卷压缩

    Python实现zip分卷压缩 使用 zipfile 库 查看 官方中文文档 利用 Python 压缩 ZIP 文件,我们第一反应是使用 zipfile 库,然而,它的官方文档中却明确标注" ...

  3. python3的zip函数

    zip函数接受任意多个可迭代对象作为参数,将对象中对应的元素打包成一个tuple,然后返回一个可迭代的zip对象. 这个可迭代对象可以使用循环的方式列出其元素 若多个可迭代对象的长度不一致,则所返回的 ...

  4. python3 解决zip解压中文乱码问题,亲测可用, ZipFile

    中文乱码是个很头疼的问题,找了好久都没用找到解决办法 后来也忘了在哪儿找到的解决办法, 很久以前了,但不可行, 解决了思路 在源码里面想要修改内容 if flags & 0x800: # UT ...

  5. python3中zip()函数的用法

    >>>a = [1,2,3] >>> b = [4,5,6] >>> c = [4,5,6,7,8] >>> zipped = ...

  6. python3 的 zip

    准备放弃生命中这4个小时,然后翻开了python,人生苦短,音乐和python才味甘 1. zip  可以看到zip两个列表,返回一个元组的列表,但是它是个可迭代的对象,得用list才能调用显示: 2 ...

  7. python3中zip对象的使用

    zip(*iterables) zip可以将多个可迭代对象组合成一个迭代器对象,通过迭代取值,可以得到n个长度为m的元组.其中n为长度最短可迭代对象的元素个数,m为可迭代对象的个数.并且每个元组的第i ...

  8. python3中zip()的用法

    zip函数接受任意多个可迭代对象作为参数,将对象中对应的元素打包成一个tuple,然后返回一个可迭代的zip对象. 这个可迭代对象可以使用循环的方式列出其元素 若多个可迭代对象的长度不一致,则所返回的 ...

  9. python3 的zip()函数

    重点 https://blog.csdn.net/qq826364410/article/details/78259796 为啥会出现几个两个空列表????

随机推荐

  1. [luoguP2038] 无线网络发射器选址(模拟)

    传送门 又是个模拟水题,考虑边界就好,连long long都不用开. ——代码 #include <cstdio> #include <iostream> int n, d, ...

  2. hdu 2089 记忆化搜索写法(数位dp)

    /* 记忆化搜索,第二维判断是否是6 */ #include<stdio.h> #include<string.h> #define N 9 int dp[N][2],digi ...

  3. 【ZJOI2017 Round2练习&BZOJ4827】D1T3 gift(FFT)

    题意: 思路:可以看出题目所要最小化的是这样一个形式: 拆出每一项之后发现会变化的项只有sigma a[i]*b[i+t]与c^2,c*(a[i]-b[i]) c可以在外层枚举,剩下的只有sigma ...

  4. 作DJANGO ORM时的一些最佳实践

    还是国外的正规,都在作DJANGO ORM的解藕化工作了. 外键不用,多对多,一对多,不用. 参考URL: http://scottlobdell.me/2015/01/sql-database-be ...

  5. 5-46 新浪微博热门话题 (30分)——unfinished HASH

    5-46 新浪微博热门话题   (30分) 新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题.新浪微 ...

  6. 洛谷——P1720 月落乌啼算钱

    题目背景 (本道题目木有以藏歌曲……不用猜了……) <爱与愁的故事第一弹·heartache>最终章. 吃完pizza,月落乌啼知道超出自己的预算了.为了不在爱与愁大神面前献丑,只好还是硬 ...

  7. MySQL: How to support full Unicode in MySQL databases

    How to support full Unicode in MySQL databases Published 30th July 2012 · tagged with MySQL, securit ...

  8. automaticallyAdjustsScrollViewInsets 使用

    automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性) @当我们在一个UIViewController中同时创建2个tableView的时候,如果把 ...

  9. 【CODEFORCES】 D. CGCDSSQ

    D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  10. Codeforces Round #327 (Div. 2)C. Median Smoothing 构造

    C. Median Smoothing   A schoolboy named Vasya loves reading books on programming and mathematics. He ...