python 字符串/列表/元组/字典之间的相互转换

一.字符串str与列表list

1.字符串转列表

字符串转为列表list,可以使用str.split()方法,split方法是在字符串中对指定字符进行切片,并返回一个列表,示例代码如下:

1

2

3

4

5

6

7

8

9

10

扫描二维码关注公众号,回复: 8587663 查看本文章

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

# !usr/bin/env python

# -*- coding:utf-8 _*-

"""

@Author:何以解忧

@Blog(个人博客地址): shuopython.com

@WeChat Official Account(微信公众号):猿说python

@Github:www.github.com

@File:python_data.py

@Time:2019/9/20 20:45

@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

str1 = "hello word 猿说python python教程"

print(str1)                 # 输出字符串

print(type(str1))           # 输出数据类型:

print(len(str1))            # 输出字符串长度

print("***"*20)             # 小敲门:直接打印60个*

#根据空格切片

list1 = str1.split(" ")     # 对字符串中的空格(' ')进行切片,返回值是一个列表list并赋值给list1

print(list1)                # 输出列表数据

print(type(list1))          # 输出数据类型:类型

print(len(list1))           # 输出列表长度(列表的数据个数)

print("***"*20)             # 小敲门:直接打印60个*

#根据字符'p'切片

list1 = str1.split("p")     # 对字符串中的'p'进行切片,返回值是一个列表list并赋值给list1

print(list1)                # 输出列表数据

print(type(list1))          # 输出数据类型:类型

print(len(list1))           # 输出列表长度(列表的数据个数)

print("***"*20)             # 小敲门:直接打印60个*

#根据字符'o'切片

list1 = str1.split("o")     # 对字符串中的'o'进行切片,返回值是一个列表list并赋值给list1

print(list1)                # 输出列表数据

print(type(list1))          # 输出数据类型:类型

print(len(list1))           # 输出列表长度(列表的数据个数)

输出结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

hello word 猿说python python教程

<class 'str'>

28

************************************************************

['hello', 'word', '猿说python', 'python教程']

<class 'list'>

4

************************************************************

['hello word 猿说', 'ython ', 'ython教程']

<class 'list'>

3

************************************************************

['hell', ' w', 'rd 猿说pyth', 'n pyth', 'n教程']

<class 'list'>

5

 

2.列表转字符串

列表转为字符串需要使用”.join()方法,join()方法可以直接将列表转为一个字符串,示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

list1 = ["hello", "word", "猿说python", "python教程"]

print(list1)                 # 输出字符串

print(type(list1))           # 输出数据类型:

print(len(list1))            # 输出字符串长度

print("***"*20)             # 小敲门:直接打印60个*

#根据空格切片

str1 = "".join(list1)      # 对字符串中的空格(' ')进行切片,返回值是一个列表list并赋值给list1

print(str1)                # 输出列表数据

print(type(str1))          # 输出数据类型:类型

print(len(str1))           # 输出列表长度(列表的数据个数)

输出结果:

1

2

3

4

5

6

7

['猿说python', 'word', 'python教程', 'hello']

<class 'list'>

4

************************************************************

猿说pythonwordpython教程hello

<class 'str'>

25

 

二.字符串str与字典dict

1.字符串转字典

将字符串转为字典可以通过内置函数eval()完成,对于内置函数eval()的使用,在后面的文章还会有详细讲解,今天先简单了解一下:

1

2

3

4

5

6

7

8

9

10

11

12

# 注意单引号和双引号的配合使用

str1 = '{"name":"zhangsan","age":18,"sing_dog":False }'

print(str1)

print(type(str1))

print(len(str1))

print("***"*20) # 小敲门:直接打印60个*

dict1 = eval(str1) # 强制将字符串str转为字典dict

print(dict1)

print(type(dict1))

print(len(dict1))

输出结果:

1

2

3

4

5

6

7

{"name":"zhangsan","age":18,"sing_dog":False }

<class 'str'>

46

************************************************************

{'name': 'zhangsan', 'age': 18, 'sing_dog': False}

<class 'dict'>

3

 

2.字典转字符串

将字典转为字符串可以直接通过str()类型强制转换即可,示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

dict1 = {"name":"zhangsan","age":18,"sing_dog":False }

print(dict1)

print(type(dict1))

print(len(dict1))

print("***"*20) # 小敲门:直接打印60个*

str1 = str(dict1) # 强制将字典dict转为字符串str

print(str1)

print(type(str1))

print(len(str1))

输出结果:

1

2

3

4

5

6

7

{'name': 'zhangsan', 'age': 18, 'sing_dog': False}

<class 'dict'>

3

************************************************************

{'name': 'zhangsan', 'age': 18, 'sing_dog': False}

<class 'str'>

50

 

三.列表list与字典dict

1.列表转字典

列表转为字典不能通过dict()强制转换,但是可以通过内置函数zip()完成,具体代码如下:

1

2

3

4

5

6

7

list1 = ["hello", "word", "猿说python", "python教程"]

list2 = ["a","b","c","d","e","f","g"]

dict1 = dict(zip(list1,list2))

print(dict1)

print(type(dict1))

print(len(dict1))

输出结果:

1

2

3

{'hello': 'a', 'word': 'b', '猿说python': 'c', 'python教程': 'd'}

<class 'dict'>

4

注意:内置函数zip 是将两个列表的数据两两组合形成键值对,构成字典;如果两个列表的长度不一致时,多出的元素在另一个列表无匹配的元素时就不展示多出的元素。

 

2.字典转列表

可以通过list()方法强制将字典中的key 或者 value转为列表,示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

dict1 = {"name":"zhangsan","age":18,"sing_dog":False }

# 强制将字典dict中的keys转为列表

list1= list(dict1.keys())

print(list1)

print(type(list1))

print(len(list1))

print("***"*20) # 小敲门:直接打印60个*

# 强制将字典dict中的values转为列表

list2 = list(dict1.values())

print(list2)

print(type(list2))

print(len(list2))

输出结果:

1

2

3

4

5

6

7

['name', 'age', 'sing_dog']

<class 'list'>

3

************************************************************

['zhangsan', 18, False]

<class 'list'>

3

 

 

猜你喜欢:

1.python  字符串

2.python 列表

3.python 元组

4.python 字典

 

转载请注明猿说Python » python 字符串(str)/列表(list)/元组(tuple)/字典(dict)之间的相互转换


猜你喜欢

转载自blog.51cto.com/14531342/2466527