主頁(yè) > 知識(shí)庫(kù) > Python 實(shí)現(xiàn)list,tuple,str和dict之間的相互轉(zhuǎn)換

Python 實(shí)現(xiàn)list,tuple,str和dict之間的相互轉(zhuǎn)換

熱門標(biāo)簽:百度AI接口 企業(yè)做大做強(qiáng) Win7旗艦版 呼叫中心市場(chǎng)需求 語(yǔ)音系統(tǒng) 客戶服務(wù) 硅谷的囚徒呼叫中心 電話運(yùn)營(yíng)中心

1、字典(dict)

dict = {‘name': ‘Zara', ‘a(chǎn)ge': 7, ‘class': ‘First'}

1.1 字典——字符串

返回:

print type(str(dict)), str(dict)

1.2 字典——元組

返回:(‘a(chǎn)ge', ‘name', ‘class')

print tuple(dict)

1.3 字典——元組

返回:(7, ‘Zara', ‘First')

print tuple(dict.values())

1.4 字典——列表

返回:[‘a(chǎn)ge', ‘name', ‘class']

print list(dict)

1.5 字典——列表

print dict.values

2、元組

tup=(1, 2, 3, 4, 5)

2.1 元組——字符串

返回:(1, 2, 3, 4, 5)

print tup.__str__()

2.2 元組——列表

返回:[1, 2, 3, 4, 5]

print list(tup)

2.3 元組不可以轉(zhuǎn)為字典

3、列表

nums=[1, 3, 5, 7, 8, 13, 20];

3.1 列表——字符串

返回:[1, 3, 5, 7, 8, 13, 20]

print str(nums)

3.2 列表——元組

返回:(1, 3, 5, 7, 8, 13, 20)

print tuple(nums)

3.3 列表不可以轉(zhuǎn)為字典

4、字符串

4.1 字符串——元組

返回:(1, 2, 3)

print tuple(eval("(1,2,3)"))

4.2 字符串——列表

返回:[1, 2, 3]

print list(eval("(1,2,3)"))

4.3 字符串——字典

返回:

print type(eval("{'name':'ljq', 'age':24}"))

補(bǔ)充:python入門之路:一個(gè)小錯(cuò)誤,str變tuple

筆者在編程的時(shí)候發(fā)現(xiàn),原先定義的str字符串在傳遞和引用的時(shí)候會(huì)莫名其妙改變類型,變成tuple。

import random 
class get_Veri(object): 
  def random_color(self):
    random_color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
    return random_color
 
  def random_num(self):
    random_num = str(random.randint(0, 9))
    return random_num
 
  def random_lowerchr(self):
    random_lowerchar=chr(random.randint(97, 122))
    return random_lowerchar
 
  def random_upperchr(self):
    random_upperchr = chr(random.randint(65, 90))
    return random_upperchr
 
  def random_char(self):
    random_char = random.choice([get_Veri.random_num(self), get_Veri.random_upperchr(self), get_Veri.random_lowerchr(self)])
    print(random_char)
    print(type(random_char))
    return random_char

這里random_char函數(shù)輸出一個(gè)隨機(jī)字符串,可以看到type類型為:

class 'str'>

在另一個(gè)文件中進(jìn)行引用:

from random_data.py import get_Veri 
 
get_veri=get_Veri()
random_char = get_veri.random_char(),
print(random_char)
print(type(random_char))

發(fā)現(xiàn)random_char的type類型已經(jīng)發(fā)生改變:

class 'tuple'>

只是一個(gè)簡(jiǎn)單的賦值,為什么會(huì)發(fā)生改變?

原因是在賦值的時(shí)候多加了一個(gè)逗號(hào)。

這個(gè)逗號(hào)讓編譯器執(zhí)行的時(shí)候理解為("str",)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 解決python中set與dict的無序問題
  • 詳解Python 中的 defaultdict 數(shù)據(jù)類型
  • python的dict判斷key是否存在的方法
  • Python字典dict常用方法函數(shù)實(shí)例
  • python Yaml、Json、Dict之間的轉(zhuǎn)化
  • Python xmltodict模塊安裝及代碼實(shí)例
  • python 中dict的元素取值操作

標(biāo)簽:長(zhǎng)沙 濟(jì)南 海南 喀什 山西 崇左 山西 安康

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python 實(shí)現(xiàn)list,tuple,str和dict之間的相互轉(zhuǎn)換》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266