我有一组带有一些键值对的词典。我想知道最有效的方式将它们分成两半,然后对每组进行一些处理。如果我有字典A,B,C,D,我想得到结果集:(A,B),(A,C) ,(A,d),而不是其余组(C,d),(B,d),(B,C)Python中的分割集
0
A
回答
1
可以是这样的:
例如:
In [17]: from itertools import *
In [18]: lis=('a','b','c','d')
In [19]: for x in islice(combinations(lis,2),len(lis)-1):
print x,
....:
....:
('a', 'b') ('a', 'c') ('a', 'd')
5
itertools
和单线通常属于同一句话:
>>> import itertools
>>> s = ['A', 'B', 'C', 'D']
>>> i = itertools.product(s[0], s[1:])
>>> list(i)
[('A', 'B'), ('A', 'C'), ('A', 'D')]
+0
打败我吧。 +1 –
0
试试这个:
l = ['a','b','c','d']
def foo(l):
s0 = None
for i in l:
if s0 is None:
s0=i
continue
yield (s0,i)
for k in foo(l):
print k
输出:
('a', 'b')
('a', 'c')
('a', 'd')
0
恕我直言,itertools
显然是矫枉过正:
>>> s = 'ABCDE'
>>> [(s[0], x) for x in s[1:]]
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E')]
>>>
相关问题
- 1. 麻烦在Python中分割和分割
- 2. python中的分割线
- 3. Python中的分割函数
- 4. Python中的多重分割
- 5. .NET:集中分割配置
- 6. 分割为python
- 7. Backbone.js分割集合分块
- 8. 在Python中分割文本
- 9. 在Python中分割列表
- 10. Python中分割两行零?
- 11. 在python中分割句子
- 12. 在python中分割列表
- 13. 在Python中分割整数?
- 14. 在Python中分割图像
- 15. 在Python中分割数组
- 16. 子集ESET的/分割ESET
- 17. 通过分割'\ t'来分割python中的列表元素
- 18. 分割数组python
- 19. 分割列表Python
- 20. 分割python列表
- 21. 分割错误Python
- 22. Python分割错误?
- 23. Python分割错误?
- 24. python分割函数
- 25. Python的 - 字符串分割
- 26. 分割后的Python龟图
- 27. Python的分割字符串
- 28. 的Python ::分割字符串
- 29. 分割字符串Python的
- 30. Python上的分割目录
我不知道你在问什么。你能用代码展示你的一套词典和输出的例子吗? – deadly