组合数据类型练习,英文词频统计实例上
1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
d={'Jackson':89,'Micheal':95,'Jordan':68} #增 d['Andrew']=86 print(d) #删 del(d['Jordan']) print(d) #改 print(d.get('Jackson')) print(d.keys()) #查 print(d.get('Micheal',80)) print(d.values()) print(d.items()) print(d.pop('Micheal',90))
2.列表,元组,字典,集合的遍历。
s=list('123456654321') t=tuple('123456654321') sl=set('123456654321') d=['Jackson','Brain'] i=[85,95] z=dict(zip(d,i)) for i in s: print(i,end='/') print() for i in t: print(i,end='/') print() for i in sl: print() for i in z: print(i,z[i])
总结列表,元组,字典,集合的联系与区别。
(1)列表是任意对象的序列,列表用方括号表示。
(2)元组是将一组值打包到一个对象中,用圆括号表示。可以随时插入,删除;而元组一旦确认就不能够再更改。元组在定义过程中,字符串必须用单引号‘扩起来。
(3)字典就是一个关联数组,由键和值组成,其中包含通过键的对象。用大括号表示。与集合相比,比集合访问方便。
(4)与列表和元组不同,集合是无序的,也不能通过索引进行访问。而且集合中的元素不能重复。
3.英文词频统计实例
待分析字符串分解提取单词
大小写 txt.lower()
分隔符'.,:;?!-_’
单词列表
news='''Beijing's secondary home market is cooling down as the number of people who signed contracts to buy used homes declined. In the third quarter of this year, the figure has fallen to the lowest it's been in the past 10 quarters, and average price of the used home also has decreased for the first time from 2015, according a report published by a real estate agency 5i5j. Up to Sept 23, people who live in Beijing signed 20,600 contracts to buy the used home in the third quarter, the figure dropping 43.7 percent compared to the last quarter and 73.7 percent compared to the same period last year, the report said. According to 5i5j, in the past three years, a record high of used home transaction volume were 78,500 units set in the third quarter of 2016. After that, the number of transaction volume fell quarter-on-quarter. During the past four quarters, the figure dropped 25.6 percent, 12.7 percent, 28.2 percent and 43.7 percent, respectively. Up to the third quarter of this year, the transaction volume declined over 70 percent. Compared to the significant drop of sale transaction, the price of the used home drop was relatively mild in the Beijing. After two-round of property tightening measures announced in Sept 30 of last year and March 17 of this year, Beijing's used home price rose until it stabilized in the second quarter of this year. Even in the first quarter of this year, the city's used home price surged to a record high. However, in the third quarter of this year, Beijing's used home price fell 5 percent compared to last quarter. The biggest decline of used home price is in Beijing's Xicheng district because old homes struggles to maintain high value, and the homes with good education resources are under control in the existent property tightening measures. According to another real estate agency Lianjia, 61.4 percent homes in Xicheng district are used home, and these homes were built more than 20 years ago. As the city's home market remains sluggish, the transaction period is lengthened. In August, the transaction period reached 64.4 days that reached peak-days from 2012. Compared to the market, the transaction period lengthened to 25 days, data from Lianjia. Caixin news website citing an industry insider said the slow approval of transfer of home ownership and slow speed of home owners to sale their properties is behind the transaction time-lapse. Hu Jinghui, vice-president of 5i5j, said, as the used home price declined to the lowest in the past two quarters, and some consumers with rigid demands are planning to purchase homes, the transaction volume will perk up slightly. However, because the tightening measures will not ease, the improvement in the home market will be limited. A property broker in Beijing said to Caixin that after the weakest selling period in July, the volume of people inquiring about ask homes, looking at home and transactions are increasing, but the final transaction price is still in low point. The man said, the normal price of a two-bedroom used home in Dongdaqiao subway station is 75,000 yuan ($11,339) per square meter, but the final price is 68,000 yuan per square meter. Moreover, according to the man, the volume of people looking at properties increased three times, compared to July in Xicheng district. Some people wanting to buy homes with good education resources are back in the district. Data from Lianjia showed that used home price in Beijing fell four consecutive months from April of this year. In August, the used home price dropped 0.58 percent month-on-month, and compared to the peak in April, the price fell 9.7 percentage points. The retreat price showed property owners are considering to give a reasonable home price to the market, Lianjia said. This round of housing market recovery is a slow process; Judging from the current situation, new measures to ease the tightening house market are impossible to publish in the near future, said Guo Yi, marketing director of real estate consultancy Yahao.''' #大小写 news=news.lower() #去标点 for i in ',.': news=news.replace(i,' ') print(news) #转为列表 words=news.split(' ') print(words)