简体   繁体   中英

Python start line with a letter when printing a list?

I want my code to print as

a. [value1]
b. [value2]
c. [value3]
d. [value4]
e. [value5]
f. [value6]

I currently have a simple counter set up in the form of

counter = 0
for key in sorted(word_counter):
    counter+=1
    print(counter, key, word_counter[key])

Word_counter is for another function I've already written that's finished I just want to fix how it prints. It's currently using numbers at the start but I want it to use letters instead like first example.

Forgot to add, once it hits z I want the next letters to be aa. bb. etc like so:

x. [value24]
y. [value25]
z. [value26]
aa. [value27]
bb. [value28]
cc. [value29]

each iteration through the alphabet adds a letter to the end.

This is pretty straightforward:

>>> import string
>>> from itertools import cycle
>>> x = range(100)
>>> letters = string.ascii_lowercase
>>> m = len(letters)
>>> for i, (let, e) in enumerate(zip(cycle(letters), x)):
...     print("{}. [{}]".format(let*(i//m+1), e))
...
a. [0]
b. [1]
c. [2]
d. [3]
e. [4]
f. [5]
g. [6]
h. [7]
i. [8]
j. [9]
k. [10]
l. [11]
m. [12]
n. [13]
o. [14]
p. [15]
q. [16]
r. [17]
s. [18]
t. [19]
u. [20]
v. [21]
w. [22]
x. [23]
y. [24]
z. [25]
aa. [26]
bb. [27]
cc. [28]
dd. [29]
ee. [30]
ff. [31]
gg. [32]
hh. [33]
ii. [34]
jj. [35]
kk. [36]
ll. [37]
mm. [38]
nn. [39]
oo. [40]
pp. [41]
qq. [42]
rr. [43]
ss. [44]
tt. [45]
uu. [46]
vv. [47]
ww. [48]
xx. [49]
yy. [50]
zz. [51]
aaa. [52]
bbb. [53]
ccc. [54]
ddd. [55]
eee. [56]
fff. [57]
ggg. [58]
hhh. [59]
iii. [60]
jjj. [61]
kkk. [62]
lll. [63]
mmm. [64]
nnn. [65]
ooo. [66]
ppp. [67]
qqq. [68]
rrr. [69]
sss. [70]
ttt. [71]
uuu. [72]
vvv. [73]
www. [74]
xxx. [75]
yyy. [76]
zzz. [77]
aaaa. [78]
bbbb. [79]
cccc. [80]
dddd. [81]
eeee. [82]
ffff. [83]
gggg. [84]
hhhh. [85]
iiii. [86]
jjjj. [87]
kkkk. [88]
llll. [89]
mmmm. [90]
nnnn. [91]
oooo. [92]
pppp. [93]
qqqq. [94]
rrrr. [95]
ssss. [96]
tttt. [97]
uuuu. [98]
vvvv. [99]

使用字符串包。

import string

list_of_alphabets = list(string.ascii_lowercase)

for i in range(your number(not greater than 26)): print list_of_alphabets[i] #your task of printing value

import string

In [7]:     def letter(n):
   ...:         alphabet = string.ascii_lowercase
   ...:     
   ...:         count = 0 
   ...:         while count < n:
   ...:             iteration = (int(count / 26.) + 1)
   ...:             letter = alphabet[count % 26]
   ...:             yield letter * iteration
   ...:             count += 1
   ...:     
   ...:     print '\n'.join(['{0} value{1}'.format(l, i) for i, l in enumerate(letter(100))])

a value0
b value1
c value2
d value3
e value4
f value5
g value6
...
dd value29
ee value30
ff value31
gg value32
hh value33
ii value34
jj value35
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM