python中如何将字典直接变成二维数组_python基础知识(列表、字典、二维数组)...
記得改參數(shù)!!!
(1)簡(jiǎn)述列表(list)結(jié)構(gòu)的特點(diǎn)。
List(列表)
List的元素以線(xiàn)性方式存儲(chǔ),可以存放重復(fù)對(duì)象,List主要有以下兩個(gè)實(shí)現(xiàn)類(lèi):
ArrayList : 長(zhǎng)度可變的數(shù)組,可以對(duì)元素進(jìn)行隨機(jī)的訪(fǎng)問(wèn),向ArrayList中插入與刪除元素的速度慢。 JDK8 中ArrayList擴(kuò)容的實(shí)現(xiàn)是通過(guò)grow()方法里使用語(yǔ)句newCapacity = oldCapacity + (oldCapacity >> 1)(即1.5倍擴(kuò)容)計(jì)算容量,然后調(diào)用Arrays.copyof()方法進(jìn)行對(duì)原數(shù)組進(jìn)行復(fù)制。
LinkedList: 采用鏈表數(shù)據(jù)結(jié)構(gòu),插入和刪除速度快,但訪(fǎng)問(wèn)速度慢。
(2)簡(jiǎn)述字典(dict)結(jié)構(gòu)的特點(diǎn)。
序列是以連續(xù)的整數(shù)為索引,字典是以關(guān)鍵字為索引,關(guān)鍵字是任意不可變類(lèi)型,通常是字符串或數(shù)值,如果元組中只包含字符串和數(shù)字,也可以作為關(guān)鍵字。
列表不可以做關(guān)鍵字,因?yàn)榱斜砜梢杂盟饕⑶懈罨蛘遖ppend()和extend()等方法改變。
字典的結(jié)構(gòu)為:用{ }來(lái)包含所有的元素如:{‘蘋(píng)果’:‘a(chǎn)pple’,‘香蕉’:‘banana’},key-value之間用 : 分割,鍵值對(duì)和鍵值對(duì)之間用 , 分隔。
編碼題
(1)使用二維列表保存學(xué)生信息, 如表4-5所示
list1 = [[‘張璇’,‘17歲’,‘8年級(jí)3班’,‘成績(jī)4. 2’],[‘李敏’,‘15歲’,’ 男性’,‘7年級(jí)2班’,‘成績(jī)3. 4’],[‘趙四’,‘16歲’,’ 男性’,‘8年級(jí)1班’,‘成績(jī)4.0’],[‘李艷’,‘15歲’,‘女性’,’ 8年級(jí)1班’,‘成績(jī)3. 3’]]
output = open(‘data.xls’,‘w’,encoding=‘gbk’)
output.write(‘姓名\年齡\性別\年級(jí)\成績(jī)\n’)
for i in range(len(list1)):
for j in range(len(list1[i])):
output.write(str(list1[i][j]))
output.write('\t')
output.write('\n')
output.close()
(3)統(tǒng)計(jì)英文兒歌《twinkle twinkle little star》中使用到的單詞及其出現(xiàn)的次數(shù)。要求恕略單詞大小寫(xiě)的影響, 不統(tǒng)計(jì)標(biāo)點(diǎn)符號(hào)的個(gè)數(shù)。在控制臺(tái)上輸出的結(jié)果
message=‘Twinkle, twinkle, little star,How I wonder what you are!Up above the world so high,Like a diamond in the sky.Twinkle, twinkle, little star,How I wonder what you are!When the blazing sun is gone,When he nothing shines upon,Then you show your little light,Twinkle, twinkle, all the night.Twinkle, twinkle, little star,How I wonder what you are!Then the travler in the dark Thanks you for your tiny spark;How could he see where to go, If you did not twinkle so?Twinkle, twinkle, little star,How I wonder what you are!In the dark blue sky you keep, andThrough my curtains often peep. ,For you never shut your eyes,Till the morning sun does rise.Twinkle, twinkle, little star,How I wonder what you are!As your bright and tiny sparkLights the travler in the dark,Though I know not what you are,Twinkle on, please, little star.Twinkle, twinkle, little star,How I wonder what you are!’
count={}
for character in message:
count.setdefault(character,0)
count[character]=count[character]+1
print(count)
(4)在第(3)題的基礎(chǔ)上, 對(duì)英文兒歌中出現(xiàn)的單詞按照詞頻數(shù)從大到小進(jìn)行降序排列, 并在控制臺(tái)上輸出
本文地址:https://blog.csdn.net/R_Heng/article/details/107165822
希望與廣大網(wǎng)友互動(dòng)??
點(diǎn)此進(jìn)行留言吧!
總結(jié)
以上是生活随笔為你收集整理的python中如何将字典直接变成二维数组_python基础知识(列表、字典、二维数组)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 带负荷测试要求二次最小电流_开关柜设计人
- 下一篇: python3 os_Python3 标