導航:首頁 > 編程語言 > python字典累加

python字典累加

發布時間:2023-07-17 20:02:28

python列表如何轉字典 取相同key值把values相加

mobile=[['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','500','50'],['pear','android','600','60']]
mobiledict={}
foreleminmobile:
key=(elem[0],elem[1])
ifkeyinmobiledict:
mobiledict[key][0]+=int(elem[2])
mobiledict[key][1]+=int(elem[3])
else:
mobiledict[key]=[int(elem[2]),int(elem[3])]
print(mobiledict)

㈡ python 字典怎麼增加元素

直接加,比如說

fuck={}
fuck["Tom"]="Jerry"

㈢ python用字典統計不同字元的個數

這里用到了字典基本的建立,value調用,鍵值對增加,value修改,以及items()函數。

編程實現
流程:文件遍歷-除去空白——判斷字典中有無該字元——有則Value加1,無則新建為1—備櫻—按Value排序並返回

具體實現代碼如下:

#統計txt文件中的字元頻率
def countwords(txt):
stat = {}#建立字典存儲存儲字元和對應頻率
for line in txt:
line = line.strip()
if len(line) == 0:
continue
for i in range(len(line)):
#判知滾答斷有無該字元的鍵
if(line[i] in stat):
stat[line[i]]+=1
else:
stat[line[i]]=1
result=sorted(stat.items(),key = lambda x:x[1],reverse = True)#按value大小排序
return result
xyj = open('xyj.txt' ,'r',encoding = 'utf-8')#讀文件
r=countwords(xyj)#調用函搭慧數
xyj.close

㈣ python3 求字典中的指定值的和

basket_items = {'apples': 4, 'oranges': 19, 'kites': 3, 'sandwiches': 8}
fruits = ['apples', 'oranges', 'pears', 'peaches', 'grapes', 'bananas']

is_fruits_num = sum([basket_items[x] for x in basket_items if x in fruits])
not_fruits_num = sum([basket_items[x] for x in basket_items if x not in fruits])

print("水果數量:"+str(is_fruits_num))
print("非水果數量:"+str(not_fruits_num))

㈤ python 如何將字典中的value值求和

打開編輯器,寫上注釋內容。
python 中如何取得字典中所有value的值?

新建一個函數getvalue
python 中如何取得字典中所有value的值?

新建一個字典。

zidian={"country1":"america","country2":"australia","country3":"germany"}
python 中如何取得字典中所有value的值?

利用values這個方法來獲取字典中的所有Vlue值,並將結果列印出來。
python 中如何取得字典中所有value的值?

調用這個函數。

getvalue()
python 中如何取得字典中所有value的值?

選擇菜單中的「run」
python 中如何取得字典中所有value的值?

這時候我們就可以看到字典中的所有value值已經列印出來了。
python 中如何取得字典中所有value的值?

㈥ 怎麼把python字典的里的所有列表值相加

deftotal_takings(yearly_record):
sum=0
forkey,valueinyearly_record.items():
foriinvalue:
sum+=i
returnsum

㈦ python-字典

1、字典:

      兩大特點:無序,鍵唯一

      無序存儲,鍵值對的形式存儲數據

  鍵是唯一不可修改的,不能用列表做鍵

2、python中不可變類型:整形,字元串,元組

    可變類型:字典,列表

3、字典中方法:

增加:

dic1 = {'name':'alex'}

dic1 = ['age'] =18

*dic1 = {'age':18,'name':'alex'}

      dic1.setdefault() 鍵存在,不改動,返回字典相應鍵對應的值,鍵不存在,在字典中增加新的鍵值對,並返回相應的值

查找:

        通過鍵查找

          dic1.keys()列印字典中所有鍵 

  #dict1.keys['name','age']  --轉換成列表:list(dic1.keys())

          dic1.values()列印字典中所有值

          dic1.items()列印所有鍵值對

修改:

          直接賦值

dic3= {'name':'alex','age':18}

dic4 = {'sex':'male','age':36}

          dic3.update(dic4)    #有相同的key,值會修改

刪除:

          dic.clear()    #清空字典

          del dic['name'] #刪除字典中指定鍵值對

          dic.pop('age')#刪除字典中指定鍵值對,並返回該鍵值對的值

          dic.popitem()  #隨機刪除鍵值對,並以元組方式返回

其他操作涉及的方法:

dic1 =dict.formkeys(['host1','host2'],'test')#{'host1':'test','host2':'test'}

dic1 =dict.formkeys(['host1','host2','host3'],['test1','test2'])#{'host1':['test1','test2'],'host2':['test1','test2'],'host3':['test1','test2']}

dic1['host2'][1] = 'test3'   #{'host3':['test1''test3'],'host2':['test1''test3'],'host1':['test1''test3']}

      字典的嵌套:

      字典的排序:

      字典的遍歷:

字元串的操作

a = '123'

b= 'abc'

c = a+b #123abc

c='****'.join([a,b])#123****abc

st = 'hello kitty{name} is {age}'

    st.count('l')    #2    統計元素個數

      st.captialize() #Hello kitty 首字母大寫

      st.center(50,'-')#--------hello kitty --------居中

      st.endswith('tty3')#判斷是否以某個內容結尾

      st.startswith('he')#判斷是否以某個內容開頭

      st.find('t') #8  查找第一個元素,並返回索引,不存在是返回-1

      st.format(name  = 'alex',age= 37)#hello kitty alex is 37

      st.format_map({'name' :'alex','age':27})#hello kitty alex is 27

      st.index('t') #8 返回索引,找不到報錯

『ab'.isalnum()

'123'.isdigit()

閱讀全文

與python字典累加相關的資料

熱點內容
php和類名相同的方法 瀏覽:360
香港台灣dns伺服器地址列表 瀏覽:537
大同app怎麼樣 瀏覽:438
php去掉特殊字元 瀏覽:387
androidapi中文合集 瀏覽:658
win7下安裝linux虛擬機 瀏覽:838
雲主機用別的伺服器嗎 瀏覽:922
黑馬買入指標源碼副圖 瀏覽:962
微軟為什麼會把伺服器放在水底 瀏覽:257
php截取字元串中文 瀏覽:21
虛擬機和編譯軟體哪個好 瀏覽:750
存儲伺服器為什麼比通用伺服器難 瀏覽:373
用php列印出前一天的時間 瀏覽:369
2010編譯方法 瀏覽:239
華為哪裡查看隱藏app 瀏覽:889
linux網卡重置 瀏覽:830
框架柱低於四米箍筋全高加密 瀏覽:694
米二如何安卓版本升級到高安卓版 瀏覽:783
安卓手機數據慢怎麼辦 瀏覽:727
雙底買賣指標公式源碼無未來函數 瀏覽:685