1. 程式人生 > >Python中list、tuple、str和dict之間的相互轉換

Python中list、tuple、str和dict之間的相互轉換

1、字典(dict)

dict = {‘name’: ‘Zara’, ‘age’: 7, ‘class’: ‘First’}

1.1 字典——字串

返回:

print type(str(dict)), str(dict)
  • 1
  • 1

1.2 字典——元組

返回:(‘age’, ‘name’, ‘class’)

print tuple(dict)
  • 1
  • 1

1.3 字典——元組

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

print tuple(dict.values())
  • 1
  • 1

1.4 字典——列表

返回:[‘age’, ‘name’, ‘class’]

print list(dict)
  • 1
  • 1

1.5 字典——列表

print dict.values

2、元組

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

2.1 元組——字串

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

print tup.__str__()
  • 1
  • 1

2.2 元組——列表

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

print list(tup)
  • 1
  • 1

2.3 元組不可以轉為字典

3、列表

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

3.1 列表——字串

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

print str(nums)
  • 1
  • 1

3.2 列表——元組

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

print tuple(nums)
  • 1
  • 1

3.3 列表不可以轉為字典

4、字串

4.1 字串——元組

返回:(1, 2, 3)

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

4.2 字串——列表

返回:[1, 2, 3]

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

4.3 字串——字典

返回:

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

相關推薦

Python程式設計:namedtuple命名元組dict字典相互轉換

from collections import namedtuple dct = { "name": "Tom", "age": 24 } Person = namedtuple("

.mat,.txt,.csv 資料轉換為weka的arff格式及matlabWeka之間相互轉換格式

function r = CSVtoARFF (data, relation, type) % csv to arff file converter % load the csv data [rows cols] = size(data); % op

Pythonlisttuplestrdict之間相互轉換

1、字典(dict) dict = {‘name’: ‘Zara’, ‘age’: 7, ‘class’: ‘First’} 1.1 字典——字串 返回: print type(str(dict)), str(dict)111.2 字典——元組 返回:(‘age’, ‘na

pythonlisttupledict,set的區別及聯絡

我在python學習中遇到的一個與其他語言不太相同的一點就是list.tuple.dict.set這幾個資料型別的區別及聯絡。昨天學了,為防忘記,今天就來現學現賣。 一、list          俗稱列表,是一種有序集合。也就是說,list中的資料排列是有順序的。可以

Android 資原始檔圖片轉 Bitmap Drawable 以及相互轉換的方法

Android 圖片轉換的方法總結: 一、Bitmap 轉換成 Drawable 對 Bitmap 進行強制轉換 Drawable drawable = new BitmapDrawable(bmp); 二、Drawable 轉換成 Bitmap 方法一 通過 BitmapFa

Android根據手機的解析度dppx之間轉換

public class UnitUtil { /** * 根據手機的解析度從 dp 的單位 轉成為 px(畫素) */ public static int dp2px(Context context, float dpValue) { fina

UTF8GBK之間相互轉換python指令碼)

import codecs def ReadFile(filePath,encoding=""):     with codecs.open(filePath,"r",encoding) as f:         return f.read() def WriteFil

Android 單位dppx之間相互轉換

style pan static float logs ati col return roi public class DensityUtil { /** * 根據手機的分辨率從 dp 的單位 轉成為 px(像素) */

利用Google Gson實現JSON字串物件之間相互轉換

最近一個專案需要用到JSON,需要在JSON字串和物件之間相互轉換,在網上找了些資料,發現google的Gson還是比較不錯的。 廢話不說,下面是簡單的例子: 先上原始碼:下載(包含jar包) Person實體類 package com.hsun.json; /** * Pe

SOS, 請教:XMLstruct之間相互轉換的問題

現在配置基本都像XML方向轉變。那麼, 1、以前是按struct大小直接寫入檔案的,反過來,就直接按struct大小讀入struct就可以了,簡潔; 2、轉XML後,涉及到struct到xml節點之間的相互轉換,這個非常多的重複程式碼。而且太繁瑣,舉個例子,假設一個配置struct有100個欄位,

xml檔案DataTable之間相互轉換

using System.Xml; using System.Xml.Serialization; /// <summary> /// 下面兩個方法實現xml

python知識整理--議可變物件不可變物件以及strlisttupledictset

可變物件:變數、list 不可變物件:str、tuple、dict、set 1、變數是可變的不用多說,變數賦值一次便改變一次。 2、list 可變物件,可對list進行如下新增刪除排序等操作。 list = [2,3,5,1,2] list.append(8) list.insert(0,

python資料結構容器(listdicttupleset)C++JAVA的匯出資料型別, 陣列

list(列表):語法:列表形如 [1, 2, 3, 4] [‘小明’,‘小紅’,] ,用中括號括住,裡面是字串、布林,每一項逗號分開。 建立 宣告變數時 中括號、項,建立一個非空的列表。 num_list = [1,2,3,4] 建立一個空列表,之後再修改

Python關於listtuple字符串的比較

用法 連接 paragraph ble 大寫 空間占用 rabl 長度 命名 List定義及常用的方法見上一篇博客。 Tuple元組tuple的定義: tuple是一個有序的元素組成的不可變對象的集合,使用小括號()表示,是可叠代對象 元組中數據的訪問

pythonlist(列表)元組array的區別-分別什麼時候使用?

list是列表,list中的元素的資料型別可以不一樣。array是陣列,陣列中的元素的資料型別必須一樣 Python中的列表(list)類似於C#中的可變陣列(ArrayList),用於順序儲存結構。它可以方便、高效的的新增刪除元素,並且列表中的元素可以是多種型別。列表很多操作都跟元組一

Python資料型別strlisttupledictset

字串 字串常見的功能 name = 'fe_cow' print name.capitalize() # 首字母大寫 :Fe_cow print name.count('_') # 統計字串出現在這個字元中的個數: 1 print name.center(

Python封裝函式之strlisttupledictset

字元 encode decode test = 'aLexaLex' v = test.find('ex',5,7) #預設從開始往後找現在設定5到7的閉開區間 print(v) #找到第一個之後,獲取其

pythonpickle使用,用於listtupledictset特殊的形式進行讀寫

自動 如果 python log char pycharm 二進制 pychar tom #特殊文件的讀寫,List tuple dict setimport pickle #數據持久性模塊myList=[1,2,3,4,5,"tom is a good boy"]path

intstrlisttupledict補充

ict () 循環 字典 gbk encoding odi 表示 ems 一、int a = 123 b = a# a與b指向同一個地址值 c = 123 d = 123 e = 123 #d、c、e當在-5~257這個範圍時,都指向一個地址值(Python優化的結果,當超

python筆記三(listtupledictset)

print 索引 names dict 追加 增刪改查 list extend clas 一、list   list的增刪改查 #增, classmates.append("nadech") #在末尾追加一個元素 classmates.insert(1,"aguilera