def number_of_cases(list_data):
from itertools import combinations_with_replacement
mylist = list_data
result = list(combinations_with_replacement(mylist, 2))
return(result)
def main():
result = number_of_cases(['a', 'b'])
print(result)
结果:
>>> main()
[('a', 'a'), ('a', 'b'), ('b', 'b')]
我不希望组合之间有任何空间......如下所示.. 如何为以下结果创建代码?
>>> a = ['a', 'b', 'c']
['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']'
>>> a = ['a', 'a']
['aa']
>>> a = [1, 2, 3, 'a']
['11', '12', '13', '1a', '21', '22', '23', '2a', '31', '32', '33', '3a', 'a1', 'a2', 'a3', 'aa']
答案 0 :(得分:-1)
你需要str()元素将它们全部作为字符串,然后加入结果列表的每个元素。
def number_of_cases(list_data):
from itertools import combinations_with_replacement
list_data = [str(data) for data in list_data]
mylist=list_data
result=list(combinations_with_replacement(mylist,2))
joined_result = []
for choice in result:
joined = ''.join(choice)
joined_result.append(joined)
return(joined_result)
def main():
result=number_of_cases(['a','b'])
print(result)
['aa','ab','bb']