python---在文本/文章中统计字符串中出现单词次数

python—在文本/文章中统计字符串中出现单词次数

>>> g
'i am xu wei bo,this is best men'
>>> print [i for i in g.split() if i]
['i', 'am', 'xu', 'wei', 'bo,this', 'is', 'best', 'men']
>>> 

原始文件

root@kali:~/python/laowangpy# ls
countenglish.py  english1.txt  log1.txt  tokenno.py
root@kali:~/python/laowangpy# cat english1.txt 
A delegation of nearly 50 people from the US state of Iowa started their 10-day visit to China on Sunday and a groundbreaking for a China-US Friendship Demonstration Farm, believed to be the first of its kind, is set for Saturday.

Terry Branstad, the new US ambassador to China, will attend the ceremony, according to a news release from the Iowa Sister States commission, a nonprofit organization founded in 1985 to manage Iowa's official relationships with foreign states and provinces. The organization was established when Branstad was governor.

The China-US Friendship Demonstration Farm, to be located in Luanping county, Hebei province, will include an educational agriculture site modeled after a farm near Maxwell, Iowa, owned by Rick and Martha Kimberley and their son Grant. The demonstration farm is a joint project of Iowa and Hebei. Its educational components will include visuals of modern agriculture technology such as machinery, and advances such as grain storage and drying, as well as test plots.

Rick Kimberley, president of Kimberley Farms Inc, told China Daily that the demonstration farm will occupy a total area of 1,214 hectares. "The first phase will be a recreation of my home and buildings," Kimberley said.

"When President Xi Jinping was at my home and farm (as vice-president, in 2012), he said that he wanted to use my farm as a model of modern agriculture to China. So they will recreate my home and buildings."

The demonstration farm will use the same modern equipment and technology that the Kimberleys use. There will be demonstration plots for seeds and demonstrations of the use of equipment.

"The farm will create an environment for learning and showcasing modern farming practices and techniques," Kimberley said. "There will also be a conference center."

After the groundbreaking, there also will be a Hebei-Iowa agriculture seminar and Branstad will host a reception, according to the news release.

In 1983, then-governor Branstad signed the Iowa-Hebei Sister State/Province agreement with then-Hebei governor Zhang Shuguang. The "relationship is one of Iowa's most famous and visible relationships since it was formally recognized by Chinese President Xi Jinping", according to a news release. In 1985, Xi, then a county leader in Hebei province, visited Iowa through the sister-state/province program.

In 2012, when Xi returned to Iowa as vice-president of China, it was his first time back to the state since 1985. During his visit, he met with several "old friends" from 1985, and made visits to new places such as the Kimberleys' farm. During that visit he declared that Chinese farms should be modeled after the Kimberley farm, according to the news release from Iowa Sister States commission.

Those attending the event include Kim Heidemann, Iowa Sister States executive director, and board members Luca Berrone, Grant Kimberley and Will Zhang, Rick Kimberley and representatives from companies and groups involved in China-US trade and relations, such as Cozen O'Connor, China-Iowa Group, Diamond V, TransOva, Sukup Manufacturing, Hy-Line International, Principal Financial, Syngenta, John Deere, Monsanto and DuPont Pioneer.
root@kali:~/python/laowangpy# 
root@kali:~/python/laowangpy# 
root@kali:~/python/laowangpy# 
root@kali:~/python/laowangpy# cat countenglish.py 
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import collections 
import os

f1 = open('english1.txt','r')
tmp = f1.read()
print '----显示原始字符串信息--------'
print tmp
f1.close()

print '------统计每次字母出现的次数------'
print [(x,tmp.count(x)) for x in set(tmp)]

print '------显示每个单词----------'
print [i for i in tmp.split()]

print '-------统计每次单词出现的次数---------'
print collections.Counter(tmp.split())

print '-------统计所有单词数量----------'
print len([i for i in tmp.split()])
print '-----------------'

root@kali:~/python/laowangpy# 
root@kali:~/python/laowangpy# 
root@kali:~/python/laowangpy# python countenglish.py 
----显示原始字符串信息--------
A delegation of nearly 50 people from the US state of Iowa started their 10-day visit to China on Sunday and a groundbreaking for a China-US Friendship Demonstration Farm, believed to be the first of its kind, is set for Saturday.

Terry Branstad, the new US ambassador to China, will attend the ceremony, according to a news release from the Iowa Sister States commission, a nonprofit organization founded in 1985 to manage Iowa's official relationships with foreign states and provinces. The organization was established when Branstad was governor.

The China-US Friendship Demonstration Farm, to be located in Luanping county, Hebei province, will include an educational agriculture site modeled after a farm near Maxwell, Iowa, owned by Rick and Martha Kimberley and their son Grant. The demonstration farm is a joint project of Iowa and Hebei. Its educational components will include visuals of modern agriculture technology such as machinery, and advances such as grain storage and drying, as well as test plots.

Rick Kimberley, president of Kimberley Farms Inc, told China Daily that the demonstration farm will occupy a total area of 1,214 hectares. "The first phase will be a recreation of my home and buildings," Kimberley said.

"When President Xi Jinping was at my home and farm (as vice-president, in 2012), he said that he wanted to use my farm as a model of modern agriculture to China. So they will recreate my home and buildings."

The demonstration farm will use the same modern equipment and technology that the Kimberleys use. There will be demonstration plots for seeds and demonstrations of the use of equipment.

"The farm will create an environment for learning and showcasing modern farming practices and techniques," Kimberley said. "There will also be a conference center."

After the groundbreaking, there also will be a Hebei-Iowa agriculture seminar and Branstad will host a reception, according to the news release.

In 1983, then-governor Branstad signed the Iowa-Hebei Sister State/Province agreement with then-Hebei governor Zhang Shuguang. The "relationship is one of Iowa's most famous and visible relationships since it was formally recognized by Chinese President Xi Jinping", according to a news release. In 1985, Xi, then a county leader in Hebei province, visited Iowa through the sister-state/province program.

In 2012, when Xi returned to Iowa as vice-president of China, it was his first time back to the state since 1985. During his visit, he met with several "old friends" from 1985, and made visits to new places such as the Kimberleys' farm. During that visit he declared that Chinese farms should be modeled after the Kimberley farm, according to the news release from Iowa Sister States commission.

Those attending the event include Kim Heidemann, Iowa Sister States executive director, and board members Luca Berrone, Grant Kimberley and Will Zhang, Rick Kimberley and representatives from companies and groups involved in China-US trade and relations, such as Cozen O'Connor, China-Iowa Group, Diamond V, TransOva, Sukup Manufacturing, Hy-Line International, Principal Financial, Syngenta, John Deere, Monsanto and DuPont Pioneer.

------统计每次字母出现的次数------
[('\n', 21), (' ', 484), ('"', 12), ("'", 4), (')', 1), ('(', 1), ('-', 13), (',', 46), ('/', 2), ('.', 22), ('1', 10), ('0', 4), ('3', 1), ('2', 5), ('5', 5), ('4', 1), ('9', 5), ('8', 5), ('A', 2), ('C', 13), ('B', 5), ('D', 8), ('G', 3), ('F', 6), ('I', 19), ('H', 8), ('K', 11), ('J', 3), ('M', 4), ('L', 3), ('O', 2), ('P', 6), ('S', 19), ('R', 3), ('U', 5), ('T', 12), ('W', 2), ('V', 1), ('X', 4), ('Z', 2), ('a', 227), ('c', 72), ('b', 36), ('e', 301), ('d', 101), ('g', 48), ('f', 49), ('i', 201), ('h', 92), ('k', 8), ('j', 2), ('m', 78), ('l', 93), ('o', 169), ('n', 213), ('q', 3), ('p', 36), ('s', 150), ('r', 172), ('u', 53), ('t', 186), ('w', 46), ('v', 27), ('y', 34), ('x', 2), ('z', 4)]
------显示每个单词----------
['A', 'delegation', 'of', 'nearly', '50', 'people', 'from', 'the', 'US', 'state', 'of', 'Iowa', 'started', 'their', '10-day', 'visit', 'to', 'China', 'on', 'Sunday', 'and', 'a', 'groundbreaking', 'for', 'a', 'China-US', 'Friendship', 'Demonstration', 'Farm,', 'believed', 'to', 'be', 'the', 'first', 'of', 'its', 'kind,', 'is', 'set', 'for', 'Saturday.', 'Terry', 'Branstad,', 'the', 'new', 'US', 'ambassador', 'to', 'China,', 'will', 'attend', 'the', 'ceremony,', 'according', 'to', 'a', 'news', 'release', 'from', 'the', 'Iowa', 'Sister', 'States', 'commission,', 'a', 'nonprofit', 'organization', 'founded', 'in', '1985', 'to', 'manage', "Iowa's", 'official', 'relationships', 'with', 'foreign', 'states', 'and', 'provinces.', 'The', 'organization', 'was', 'established', 'when', 'Branstad', 'was', 'governor.', 'The', 'China-US', 'Friendship', 'Demonstration', 'Farm,', 'to', 'be', 'located', 'in', 'Luanping', 'county,', 'Hebei', 'province,', 'will', 'include', 'an', 'educational', 'agriculture', 'site', 'modeled', 'after', 'a', 'farm', 'near', 'Maxwell,', 'Iowa,', 'owned', 'by', 'Rick', 'and', 'Martha', 'Kimberley', 'and', 'their', 'son', 'Grant.', 'The', 'demonstration', 'farm', 'is', 'a', 'joint', 'project', 'of', 'Iowa', 'and', 'Hebei.', 'Its', 'educational', 'components', 'will', 'include', 'visuals', 'of', 'modern', 'agriculture', 'technology', 'such', 'as', 'machinery,', 'and', 'advances', 'such', 'as', 'grain', 'storage', 'and', 'drying,', 'as', 'well', 'as', 'test', 'plots.', 'Rick', 'Kimberley,', 'president', 'of', 'Kimberley', 'Farms', 'Inc,', 'told', 'China', 'Daily', 'that', 'the', 'demonstration', 'farm', 'will', 'occupy', 'a', 'total', 'area', 'of', '1,214', 'hectares.', '"The', 'first', 'phase', 'will', 'be', 'a', 'recreation', 'of', 'my', 'home', 'and', 'buildings,"', 'Kimberley', 'said.', '"When', 'President', 'Xi', 'Jinping', 'was', 'at', 'my', 'home', 'and', 'farm', '(as', 'vice-president,', 'in', '2012),', 'he', 'said', 'that', 'he', 'wanted', 'to', 'use', 'my', 'farm', 'as', 'a', 'model', 'of', 'modern', 'agriculture', 'to', 'China.', 'So', 'they', 'will', 'recreate', 'my', 'home', 'and', 'buildings."', 'The', 'demonstration', 'farm', 'will', 'use', 'the', 'same', 'modern', 'equipment', 'and', 'technology', 'that', 'the', 'Kimberleys', 'use.', 'There', 'will', 'be', 'demonstration', 'plots', 'for', 'seeds', 'and', 'demonstrations', 'of', 'the', 'use', 'of', 'equipment.', '"The', 'farm', 'will', 'create', 'an', 'environment', 'for', 'learning', 'and', 'showcasing', 'modern', 'farming', 'practices', 'and', 'techniques,"', 'Kimberley', 'said.', '"There', 'will', 'also', 'be', 'a', 'conference', 'center."', 'After', 'the', 'groundbreaking,', 'there', 'also', 'will', 'be', 'a', 'Hebei-Iowa', 'agriculture', 'seminar', 'and', 'Branstad', 'will', 'host', 'a', 'reception,', 'according', 'to', 'the', 'news', 'release.', 'In', '1983,', 'then-governor', 'Branstad', 'signed', 'the', 'Iowa-Hebei', 'Sister', 'State/Province', 'agreement', 'with', 'then-Hebei', 'governor', 'Zhang', 'Shuguang.', 'The', '"relationship', 'is', 'one', 'of', "Iowa's", 'most', 'famous', 'and', 'visible', 'relationships', 'since', 'it', 'was', 'formally', 'recognized', 'by', 'Chinese', 'President', 'Xi', 'Jinping",', 'according', 'to', 'a', 'news', 'release.', 'In', '1985,', 'Xi,', 'then', 'a', 'county', 'leader', 'in', 'Hebei', 'province,', 'visited', 'Iowa', 'through', 'the', 'sister-state/province', 'program.', 'In', '2012,', 'when', 'Xi', 'returned', 'to', 'Iowa', 'as', 'vice-president', 'of', 'China,', 'it', 'was', 'his', 'first', 'time', 'back', 'to', 'the', 'state', 'since', '1985.', 'During', 'his', 'visit,', 'he', 'met', 'with', 'several', '"old', 'friends"', 'from', '1985,', 'and', 'made', 'visits', 'to', 'new', 'places', 'such', 'as', 'the', "Kimberleys'", 'farm.', 'During', 'that', 'visit', 'he', 'declared', 'that', 'Chinese', 'farms', 'should', 'be', 'modeled', 'after', 'the', 'Kimberley', 'farm,', 'according', 'to', 'the', 'news', 'release', 'from', 'Iowa', 'Sister', 'States', 'commission.', 'Those', 'attending', 'the', 'event', 'include', 'Kim', 'Heidemann,', 'Iowa', 'Sister', 'States', 'executive', 'director,', 'and', 'board', 'members', 'Luca', 'Berrone,', 'Grant', 'Kimberley', 'and', 'Will', 'Zhang,', 'Rick', 'Kimberley', 'and', 'representatives', 'from', 'companies', 'and', 'groups', 'involved', 'in', 'China-US', 'trade', 'and', 'relations,', 'such', 'as', 'Cozen', "O'Connor,", 'China-Iowa', 'Group,', 'Diamond', 'V,', 'TransOva,', 'Sukup', 'Manufacturing,', 'Hy-Line', 'International,', 'Principal', 'Financial,', 'Syngenta,', 'John', 'Deere,', 'Monsanto', 'and', 'DuPont', 'Pioneer.']
-------统计每次单词出现的次数---------
Counter({'and': 23, 'the': 18, 'to': 14, 'a': 14, 'of': 13, 'will': 12, 'as': 8, 'Iowa': 7, 'be': 7, 'Kimberley': 7, 'farm': 7, 'from': 5, 'was': 5, 'that': 5, 'in': 5, 'The': 5, 'Sister': 4, 'agriculture': 4, 'for': 4, 'news': 4, 'according': 4, 'he': 4, 'modern': 4, 'my': 4, 'demonstration': 4, 'such': 4, 'include': 3, 'Rick': 3, 'home': 3, 'first': 3, 'use': 3, 'with': 3, 'Xi': 3, 'Branstad': 3, 'is': 3, 'China-US': 3, 'States': 3, 'In': 3, '"The': 2, 'relationships': 2, 'During': 2, 'release': 2, 'China,': 2, 'Hebei': 2, 'since': 2, 'Farm,': 2, 'state': 2, 'China': 2, 'new': 2, 'Friendship': 2, 'by': 2, 'Chinese': 2, 'US': 2, '1985,': 2, 'visit': 2, 'their': 2, 'it': 2, "Iowa's": 2, 'also': 2, 'Demonstration': 2, 'province,': 2, 'release.': 2, 'an': 2, 'his': 2, 'said.': 2, 'educational': 2, 'technology': 2, 'when': 2, 'President': 2, 'after': 2, 'modeled': 2, 'organization': 2, 'ambassador': 1, 'they': 1, 'storage': 1, 'executive': 1, "O'Connor,": 1, 'director,': 1, 'founded': 1, 'Sunday': 1, 'through': 1, 'sister-state/province': 1, 'involved': 1, 'its': 1, '2012),': 1, 'equipment': 1, 'Inc,': 1, 'Grant.': 1, 'should': 1, 'environment': 1, 'board': 1, 'Grant': 1, 'Cozen': 1, 'then': 1, 'recreate': 1, 'returned': 1, 'advances': 1, 'Xi,': 1, 'farm,': 1, 'farm.': 1, 'joint': 1, 'made': 1, 'commission.': 1, 'nearly': 1, 'commission,': 1, 'trade': 1, 'attending': 1, 'one': 1, 'program.': 1, 'State/Province': 1, 'recreation': 1, 'Pioneer.': 1, 'provinces.': 1, 'visible': 1, 'drying,': 1, '50': 1, '"There': 1, 'Hebei-Iowa': 1, 'China.': 1, 'Jinping",': 1, 'declared': 1, 'set': 1, 'Kimberley,': 1, 'people': 1, 'visuals': 1, 'companies': 1, 'techniques,"': 1, 'back': 1, 'demonstrations': 1, 'vice-president,': 1, 'delegation': 1, 'Zhang': 1, 'then-governor': 1, 'John': 1, 'event': 1, 'International,': 1, "Kimberleys'": 1, 'established': 1, 'total': 1, '1,214': 1, 'There': 1, 'representatives': 1, 'Terry': 1, 'DuPont': 1, 'leader': 1, 'told': 1, 'agreement': 1, 'Daily': 1, 'showcasing': 1, 'machinery,': 1, 'Diamond': 1, 'China-Iowa': 1, 'members': 1, 'wanted': 1, 'on': 1, 'friends"': 1, 'signed': 1, 'met': 1, 'area': 1, 'foreign': 1, 'Farms': 1, 'seeds': 1, 'grain': 1, 'nonprofit': 1, '(as': 1, 'create': 1, 'Jinping': 1, 'Manufacturing,': 1, 'groups': 1, 'Kimberleys': 1, 'manage': 1, 'equipment.': 1, 'owned': 1, 'son': 1, 'county': 1, 'phase': 1, 'relations,': 1, 'Branstad,': 1, 'learning': 1, 'president': 1, 'Martha': 1, '1985.': 1, 'conference': 1, 'groundbreaking,': 1, '1985': 1, 'occupy': 1, 'plots': 1, 'there': 1, 'ceremony,': 1, 'use.': 1, 'Saturday.': 1, 'farming': 1, '1983,': 1, 'Those': 1, 'project': 1, 'Berrone,': 1, 'formally': 1, 'started': 1, 'TransOva,': 1, 'buildings,"': 1, 'So': 1, '"When': 1, '2012,': 1, 'V,': 1, 'Iowa-Hebei': 1, 'places': 1, 'Group,': 1, 'official': 1, 'Maxwell,': 1, 'Sukup': 1, 'near': 1, 'time': 1, 'county,': 1, 'believed': 1, 'visited': 1, 'at': 1, '"old': 1, 'hectares.': 1, 'site': 1, 'states': 1, 'visit,': 1, 'governor.': 1, 'host': 1, 'kind,': 1, 'Hebei.': 1, 'Hy-Line': 1, 'Financial,': 1, 'reception,': 1, 'Luanping': 1, 'After': 1, 'center."': 1, 'same': 1, 'located': 1, 'seminar': 1, 'Kim': 1, 'test': 1, 'Shuguang.': 1, 'several': 1, 'plots.': 1, 'A': 1, 'Heidemann,': 1, 'buildings."': 1, 'attend': 1, '"relationship': 1, 'Iowa,': 1, 'Syngenta,': 1, 'Zhang,': 1, 'visits': 1, 'Will': 1, 'most': 1, 'said': 1, 'Luca': 1, 'groundbreaking': 1, 'then-Hebei': 1, 'model': 1, 'recognized': 1, 'farms': 1, 'famous': 1, 'vice-president': 1, 'well': 1, 'Monsanto': 1, 'Deere,': 1, 'practices': 1, '10-day': 1, 'governor': 1, 'components': 1, 'Its': 1, 'Principal': 1})
-------统计所有单词数量----------
495
-----------------
root@kali:~/python/laowangpy# 

你可能感兴趣的:(基础知识,pyhton)