如何在列表中分配增量名稱?如何將增量名稱分配給列表中的元素?
>>> ccList
['az <[email protected]>', 'cc777 <[email protected]>', 'user11 <[email protected]>']
>>> 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
>>>
我試圖結束與
name0 = az
name1 = cc77
name2 = user11
,這樣我可以把這些名字後。我怎樣才能做到這一點?基本上,我不明白如何寫這個:
>>> for i in range(len(ccList)):
mailTuple = parseaddr(ccList[i])
cc = mailTuple[0]
"name"+str(i) = cc
....
當然,這是行不通的。謝謝!
更新
感謝大家與偉大的答案!我認爲這個清單解決方案Thomas K將在這種情況下工作;只是我沒能解決如何打印如果CC收件人是不是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'
>>>
那麼,如何修改代碼,這樣,如果有超過3個CC等,它仍然打印所有抄送收件人?
出了什麼問題只是用指數爲列表,元組等? – 2010-12-01 00:35:06
爲什麼name1比通常的名字[1]更好? – 2010-12-01 00:35:43
查找python字典或'dict'結構。這對於做這種鍵值對是很棒的。 – DGH 2010-12-01 00:38:34