python - How do I assign incremental names to elements of a list? -
how assign incremental names in list?
>>> cclist ['az <az@example.com>', 'cc777 <cc777@example.com>', 'user11 <user11@example.com>'] >>> in range(len(cclist)): mailtuple = parseaddr(cclist[i]) cc = mailtuple[0] print cc az cc777 user11 >>> >>> in range(3): name = "name"+str(i) print name name0 name1 name2 >>>
i trying end
name0 = az name1 = cc77 name2 = user11
so can refer these names later. how can this? basically, don't understand how write this:
>>> in range(len(cclist)): mailtuple = parseaddr(cclist[i]) cc = mailtuple[0] "name"+str(i) = cc ....
of course, not work. thanks!
update
thanks great answers! think list solution thomas k work in case; except not solve how print if cc recipients other 3:
>>> ccnames = [] >>> in range(len(cclist)): mailtuple = parseaddr(cclist[i]) cc = mailtuple[0] ccnames.append(cc) >>> "sender attached %s ccs %s, %s, , %s" % (len(ccnames), ccnames[0], ccnames[1], ccnames[2]) 'sender attached 3 ccs az, cc777, , user11' >>>
so how fix code if there other 3 ccs, still prints cc recipients?
as others have commented, don't want create new names this. want have list, list input consists of. can index same way indexed original.
creating list names easy:
namelist = [parseaddr(cc)[0] cc in cclist]
regarding update, should trick:
>>> ccnames = [parseaddr(cc)[0] cc in cclist] >>> "sender attached %s ccs %s, , %s" % (len(ccnames), ', '.join(ccnames[:-1]), ccnames[-1]) 'sender attached 3 ccs az, cc777, , user11'
we take of names except last, , join them single string ', ' in between each adjacent pair. substitute our final format string, putting last name @ end (this allows handle "and" neatly).
Comments
Post a Comment