誰能告訴我如何創建一個快速的Python程式,列印出所有可能的兩位數的數字組合的數字。1、2和3。沒有重復的,所以沒有11、22或33。
uj5u.com熱心網友回復:
for i in range(1, 4)。)
for k in range(1,4) 。
if i!=k:
print(str(i) str(k))
uj5u.com熱心網友回復:
你可以使用帶條件的串列理解(排除相同數字的情況)。
digits = [1, 2, 3]
output = [(x, y) for x in digits for y in digits if x !=y]
print(output)
# [(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]/span>
一個更普遍、更方便的方法(由Chris建議)是使用itertools.permutation:
import itertools
output = list(itertools.permutations([1, 2, 3], 2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321109.html
標籤:
