簡體   English   中英

想要在列表中存儲變量名稱,而不是說變量的內容

[英]Want to store variable names in list, not said variable's contents

抱歉,標題令人困惑; 讓我解釋。

因此,我編寫了一個程序,該程序使用nltk和sklearn的工具按主題對電子郵件進行分類。

這是該代碼:

#Extract Emails
tech = extract_message("C:\\Users\\Cody\\Documents\\Emails\\tech.html")
gary = extract_message("C:\\Users\\Cody\\Documents\\Emails\\gary.html")
gary2 = extract_message("C:\\Users\\Cody\\Documents\\Emails\\gary2.html")
jesus = extract_message("C:\\Users\\Cody\\Documents\\Emails\\Jesus.html")
jesus2 = extract_message("C:\\Users\\Cody\\Documents\\Emails\\jesus2.html")
hockey = extract_message("C:\\Users\\Cody\\Documents\\Emails\\hockey.html")
hockey2 = extract_message("C:\\Users\\Cody\\Documents\\Emails\\hockey2.html")
shop = extract_message("C:\\Users\\Cody\\Documents\\Emails\\shop.html")

#Build dictionary of features
count_vect = CountVectorizer()
x_train_counts = count_vect.fit_transform(news.data)

#Downscaling
tfidf_transformer = TfidfTransformer()
x_train_tfidf = tfidf_transformer.fit_transform(x_train_counts)
tf_transformer = TfidfTransformer(use_idf=False).fit(x_train_counts)
x_train_tf = tf_transformer.transform(x_train_counts)

#Train classifier
clf = MultinomialNB().fit(x_train_tfidf, news.target)

#List of the extracted emails
docs_new = [gary, gary2, jesus, jesus2, shop, tech, hockey, hockey2]

#Extract feautures from emails
x_new_counts = count_vect.transform(docs_new)
x_new_tfidf = tfidf_transformer.transform(x_new_counts)

#Predict the categories for each email
predicted = clf.predict(x_new_tfidf)

現在,我正在根據預測的標簽將每個變量存儲在適當的列表中。 我想我可以這樣做:

#Store Files in a category
hockey_emails = []
computer_emails = []
politics_emails = []
tech_emails = []
religion_emails = []
forsale_emails = []

#Print out results and store each email in the appropritate category list
for doc, category in zip(docs_new, predicted):
  print('%r ---> %s' % (doc, news.target_names[category]))
   if(news.target_names[category] == 'comp.sys.ibm.pc.hardware'):
        computer_emails.append(doc)
   if(news.target_names[category] == 'rec.sport.hockey'):
        hockey_emails.append(doc)
   if(news.target_names[category] == 'talk.politics.misc'):
       politics_emails.append(doc)
   if(news.target_names[category] == 'soc.religion.christian'):
       religion_emails.append(doc)
   if(news.target_names[category] == 'misc.forsale'):
       forsale_emails.append(doc)
   if(news.target_names[category] == 'comp.sys.ibm.pc.hardware'):
       computer_emails.append(doc)

如果要打印出這些列表之一(例如,曲棍球),我的輸出將顯示存儲在變量中的內容,而不是變量本身。

我要這個:

print(hockey_emails)

output: ['hockey', 'hockey2']

但是我得到了這個:

 output: ['View View online click here Hi Thanks for signing up as a EA SPORTS NHL insider You ll now receive all of the latest and greatest news and info at this e mail address as you ve requested EA com If you need technical assistance please contact EA Help Privacy Policy Our Certified Online Privacy Policy gives you confidence whenever you play EA games To view our complete Privacy and Cookie Policy go to privacy ea com or write to Privacy Policy Administrator Electronic Arts Inc Redwood Shores Parkway Redwood City CA Electronic Arts Inc All Rights Reserved Privacy Policy User Agreement Legal ActionsMark as UnreadMark as ReadMark as SpamStarClear StarArchive Previous Next ', 'View News From The Hockey Writers The Editor s Choice stories from The Hockey Writers View this email in your browser edition Recap Stars Steamroll Predators By Matt Pryor on Dec am As the old Mary Chapin Carpenter song goes Sometimes you re the windshield Sometimes you re the bug It hasn t happened very often this season but the Dallas Stars had a windshield Continue Reading A Review of Years in Blue and White Damien Cox One on One By Anthony Fusco on Dec pm The Toronto Maple Leafs are one of the most storied and iconic franchises in the entire National Hockey League They have a century of history that spans all the way back to the early s When you have an Continue Reading Bruins Will Not Miss Beleskey By Kyle Benson on Dec am On Monday it was announced that Matt Beleskey will miss the next six weeks due to a knee injury he sustained over the weekend in a game against the Buffalo Sabres Six weeks is a long stint to be without a potential top Continue Reading Recent Articles Galchenyuk Injury Costly for CanadiensFacing Off Picking Team Canada for World JuniorsAre Johnson s Nomadic Days Over Share Tweet Forward Latest News Prospects Anaheim Ducks Arizona Coyotes Boston Bruins Buffalo Sabres Calgary Flames Carolina Hurricanes Chicago Blackhawks Colorado Avalanche Columbus Blue Jackets Dallas Stars Detroit Red Wings Edmonton Oilers Florida Panthers Los Angeles Kings Minnesota Wild Montreal Canadiens Nashville Predators New Jersey Devils New York Islanders New York Rangers Philadelphia Flyers Pittsburgh Penguins Ottawa Senators San Jose Sharks St Louis Blues Tampa Bay Lightning Toronto Maple Leafs Vancouver Canucks Washington Capitals Winnipeg Jets Copyright The Hockey Writers All rights reserved You are receiving this email because you opted in at The Hockey Writers or one of our Network Sites Our mailing address is The Hockey Writers Victoria Ave St Lambert QC J R R CanadaAdd us to your address book unsubscribe from this list update subscription preferences ActionsMark as UnreadMark as ReadMark as SpamStarClear StarArchive Previous Next ']

我以為這很簡單,但是我坐在這里撓頭。 這有可能嗎? 我應該使用其他內容而不是列表嗎? 這可能很簡單,我只是一片空白。

您必須自己跟蹤名稱,Python不會幫您這樣做。

names = 'gary gary2 Jesus jesus2 shop tech hockey hockey2'.split()
docs_new = [extract_message("C:\\Users\\Cody\\Documents\\Emails\\%s.html" % name)
            for name in names]

for name, category in zip(names, predicted):
    print('%r ---> %s' % (name, news.target_names[category]))
    if (news.target_names[category] == 'comp.sys.ibm.pc.hardware'):
        computer_emails.append(name)

不要這樣 使用字典來保存您的電子郵件收藏,並且您可以在想知道是什么時打印字典鍵。

docs_new = dict()
docs_new["tech"] = extract_message("C:\\Users\\Cody\\Documents\\Emails\\tech.html")
docs_new["gary"] = extract_message("C:\\Users\\Cody\\Documents\\Emails\\gary.html")
etc.

遍歷字典時,您會看到按鍵。

for doc, category in zip(docs_new, predicted):
    print('%s ---> %s' % (doc, news.target_names[category]))

(更多字典基礎知識:要遍歷字典值, docs_newdocs_new.values()替換上面的docs_new.values() ;或將docs_new.items()用作鍵和值。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM