我正在使用Python 2.7.12
我有以下列表:
t = [1,2,3,4,5]
我希望得到以下输出:
1+1, 1+2, 1+3, 1+4, 1+5, 2+2, 2+3, 2+4, 2+5, 3+3, 3+4, 3+5, 4+4, 4+5
我试过了:
zip(t,t[1:])
但输出是:
[(1, 2), (2, 3), (3, 4), (4, 5)]
然后,我也试过了:
zip(t,t)
但输出是:
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
答案 0 :(得分:1)
import itertools
t = ('1','2','3','4','5')
for t1, t2 in itertools.product(t, t):
if t1 <= t2:
print('%s + %s' % (t1, t2))
答案 1 :(得分:1)
for i in list1:
for j in list1[i-1:]:
print str(i)+"+"+str(j)
答案 2 :(得分:1)
您可以使用两个loops
来迭代list
,如下所示:
t = ('1', '2', '3', '4', '5')
result = []
for i in xrange(len(t)-1):
for j in xrange(i, len(t)):
result.append(t[i] + '+' + t[j])
print ', '.join(result)
输出:
1+1, 1+2, 1+3, 1+4, 1+5, 2+2, 2+3, 2+4, 2+5, 3+3, 3+4, 3+5, 4+4, 4+5
答案 3 :(得分:0)
你可以试试这个:
<div>Here is some text with a note here
<sup class="ref">
<a href="#note1" id="note1" class="ktooltip">Tooltip</a>
<div id="note1" class="ktooltip2">
<p>wannabe <b>tooltip</b> with a<a href="link">link</a></p>
</div>
</sup> that continues on here too.
</div>
输出:
from itertools import combinations_with_replacement
t = ('1','2','3','4','5')
sums = [i[0]+"+"+i[1] for i in list(combinations_with_replacement(t, 2))]
答案 4 :(得分:0)
假设t是一个列表而不是总是排序。
t.sort()
t_len = len(t)
output = []
for i in xrange(t_len - 1):
j = 1 if i == 0 else i
for k in xrange(j, t_len):
output.append(str(t[i]) + "+" + str(t[j]))
print output
答案 5 :(得分:0)
分三行:
list = range(1,6)
lists = [list[(i-1):] for i in list]
output = [str(i[0]) + '+' +str(j) for i in lists for j in i]