开发者

How do I assign incremental names to elements of a list?

How do I assign incremental names in a list?

>>> ccList
['az <az@example.com>', 'cc777 <cc777开发者_高级运维@example.com>', 'user11 <user11@example.com>']
>>> for i in range(len(ccList)):
    mailTuple = parseaddr(ccList[i])
    cc = mailTuple[0]
    print cc

az
cc777
user11
>>> 
>>> for i in range(3):
    name = "name"+str(i)
    print name

name0
name1
name2
>>>

I was trying to end up with

name0 = az
name1 = cc77
name2 = user11

so that I can refer to these names later. How can I do this? Basically, I don't understand how to write this:

>>> for i in range(len(ccList)):
    mailTuple = parseaddr(ccList[i])
    cc = mailTuple[0]
    "name"+str(i) = cc
    ....

Of course, this does not work. Thanks!

Update

Thanks to everyone with great answers! I think this list solution by Thomas K will work in this case; except that I could not solve how to print if cc recipients are other than 3:

>>> ccNames = []

>>> for i in range(len(ccList)):
    mailTuple = parseaddr(ccList[i])
    cc = mailTuple[0]
    ccNames.append(cc)

>>> "sender attached %s ccs which are %s, %s, and %s" % (len(ccNames), ccNames[0], ccNames[1], ccNames[2])
'sender attached 3 ccs which are az, cc777, and user11'
>>>

So how do I fix the code so that if there are other than 3 ccs, it still prints all cc recipients?


If you really want to refer to the names as name0, name1, etc. I would suggest using a dictionary:

names = {}
for i in range(len(ccList)):
  mailTuple = parseaddr(ccList[i])
  cc = mailTuple[0]
  names["name"+str(i)] = cc

Then you can access name0 with names["name0"], names["name1"], etc. This is a much safer option than mucking with exec / the locals dictionary as some other answers suggest.

Although the best solution, in my opinion, is to simply use a list as you use for mailTuple.

names = []
for i in range(len(ccList)):
  mailTuple = parseaddr(ccList[i])
  names.append(mailTuple[0])

As a side note, you should consider a better name for mailTuple as it isn't even a tuple. A suggestion from me is to simply name it mails.


As others have commented, you really don't want to create new names for this. You want to have a list, just like the list that your input consists of. Then you can index into it the same way you indexed into the original.

Creating a list with just the names is this easy:

nameList = [parseaddr(cc)[0] for cc in ccList]

Regarding the update, this should do the trick:

>>> ccNames = [parseaddr(cc)[0] for cc in ccList]
>>> "sender attached %s ccs which are %s, and %s" % (len(ccNames), ', '.join(ccNames[:-1]), ccNames[-1])
'sender attached 3 ccs which are az, cc777, and user11'

We take all of the names except the last, and join them up into a single string with ', ' in between each adjacent pair. Then we substitute that into our final format string, putting the last name at the end (this allows us to handle the "and" neatly).


Unless there's some obvious reason not to, simply use a list:

names = []
for i in range(len(ccList)):
    mailTuple = parseaddr(ccList[i])
    names.append(mailTuple[0])

names[0]  # az
names[1]  # cc77

There's a briefer way to make the list, too:

names = [parseaddr(ccaddr)[0] for ccaddr in ccList]


Try:

for i in range(len(ccList)):
    mailTuple = parseaddr(ccList[i])
    cc = mailTuple[0]
    exec("name%d=cc"%i)


You can add to the dict returned from vars():

for i in range(len(ccList)):
    mailTuple = parseaddr(ccList[i])
    cc = mailTuple[0]
    vars()["name"+str(i)] = cc

Once you do this, variables should exist with your desired names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜