我需要对以下代码进行一些更改。
from itertools import chain, combinations, product
from pprint import pprint
data = {
'uc':(1, 'ABCDEF'),
'lc':(2, 'abcdef'),
'no':(3, '123456'),
}
def main():
iters = [combinations(l,n) for n,l in data.values()]
combine_list = []
for count, group in data.values():
print('picking {} of {}'.format(count, group))
combine_list.append(list(combinations(group, count)))
combos = list(product(*combine_list))
pprint(combos)
main()
我要使用以下格式输入值,而不是给定的集合“ ABCDEF”,“ abcdef”和“ 123456”:
data = {
'uc':(1, '(A,B,C),(D,E),F'),
'lc':(2, '(a,b,c),(d,e),f'),
'no':(3, '(1,2,3),(4,5),6'),
}
,在这种情况下,代码会将()之间的值视为单个项目。我试过了,但是这样做却没有得到想要的结果。我应该怎么做才能起作用?
答案 0 :(得分:1)
现在您有一个字符串作为整体序列:
'(A,B,C),(D,E),F'
也许您想要一个元素列表,例如:
# the string '(A,B,C)' is a single element here
['(A,B,C)', '(D,E)', 'F']
或
# the tuple of strings ('A','B','C') is a single element here
[('A','B','C'),('D','E'),'F']
答案 1 :(得分:0)
您只需要访问字典并遍历项目,然后以您喜欢的格式重新排列它们即可:
data = {
'uc':(1, 'ABCDEF'),
'lc':(2, 'abcdef'),
'no':(3, '123456'),
}
for k,v in data.items():
num = v[0]
strings = [i for i in v[1]]
first = ','.join(i for i in strings[:3])
second = ','.join(i for i in strings[3:5])
third = strings[5]
data[k] = (v[0],'({}),({}),{}'.format(first,second,third))
>> {'uc': (1, '(A,B,C),(D,E),F'),
'lc': (2, '(a,b,c),(d,e),f'),
'no': (3, '(1,2,3),(4,5),6')}