天干地支纪年法

十天干:

甲乙丙丁戊己庚辛壬癸

十二地支:

子丑寅卯辰巳午未申酉戌亥

分别是:
[‘甲子’, ‘乙丑’, ‘丙寅’, ‘丁卯’, ‘戊辰’, ‘己巳’, ‘庚午’, ‘辛未’, ‘壬申’, ‘癸酉’, ‘甲戌’, ‘乙亥’, ‘丙子’, ‘丁丑’, ‘戊寅’, ‘己卯’, ‘庚辰’, ‘辛巳’, ‘壬午’, ‘癸未’, ‘甲申’, ‘乙酉’, ‘丙戌’, ‘丁亥’, ‘戊子’, ‘己丑’, ‘庚寅’, ‘辛卯’, ‘壬辰’, ‘癸巳’, ‘甲午’, ‘乙未’, ‘丙申’, ‘丁酉’, ‘戊戌’, ‘己亥’, ‘庚子’, ‘辛丑’, ‘壬寅’, ‘癸卯’, ‘甲辰’, ‘乙巳’, ‘丙午’, ‘丁未’, ‘戊申’, ‘己酉’, ‘庚戌’, ‘辛亥’, ‘壬子’, ‘癸丑’, ‘甲寅’, ‘乙卯’, ‘丙辰’, ‘丁巳’, ‘戊午’, ‘己未’, ‘庚申’, ‘辛酉’, ‘壬戌’, ‘癸亥’]
六十年一轮回
代码见下面

  1 #!/usr/bin/python3
  2 #coding=utf-8
  3 tiangan='甲乙丙丁戊己庚辛壬癸'
  4 dizhi='子丑寅卯辰巳午未申酉戌亥'
  5 t,d=[],[]
  6 for i in tiangan:
  7     t.append(i)
  8 for j in dizhi:
  9     d.append(j)
 10 result=[]
 11 i=0
 12 j=0
 13 t_to_id={
     }
 14 for ii in t:
 15     t_to_id[ii]=len(t_to_id)
 16 d_to_id={
     }
 17 for jj in d:
 18     d_to_id[jj]=len(d_to_id)
 19 id_to_t={
     vv:kk for kk,vv in t_to_id.items()}
 20 id_to_d={
     vv:kk for kk,vv in d_to_id.items()}
 21 k=0
 22 while k<60:
 23     while i<10 and j<12:
 24         result.append(id_to_t[i]+id_to_d[j])
 25         i=i+1
 26         j=j+1
 27         k+=1
 28     if j<12:
 29         assert i==10
 30         i=0
 31         while i<10 and j<12 and k<60:
 32             result.append(id_to_t[i]+id_to_d[j])
 33             i+=1
 34             j+=1
 35             k+=1
 36         if k>=60:
 37             break
 38     if i<10:
 39         assert j==12
 40         j=0
  41         while i<10 and j<12 and k<60:
 42             result.append(id_to_t[i]+id_to_d[j])
 43             i+=1
 44             j+=1
 45             k+=1
 46         if k>=60:
 47             break
 48 print(result)

你可能感兴趣的:(python,列表,算法)