262 lines
5.2 KiB
Markdown
262 lines
5.2 KiB
Markdown
---
|
||
title: 数据类型的转换
|
||
date: 2020-08-22 22:09:54
|
||
tags: [Python]
|
||
categories: [Python]
|
||
author: Anges黎梦
|
||
---
|
||
## 数据类型的转换
|
||
|
||
**类型:**数字、字符串、列表、元组、字典、集合、布尔
|
||
|
||
|
||
### 数字
|
||
> 使用 **int**、**float** 可以转换数据类型
|
||
|
||
- **int**:转换为 int 类型
|
||
|
||
**示例代码**:
|
||
```python
|
||
num_1 = 1
|
||
print(num_1, type(num_1))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
1 <class 'int'>
|
||
```
|
||
|
||
- **float**:转换为 float 类型
|
||
|
||
**示例代码**:
|
||
```python
|
||
print(float(num_1), type(float(num_1)))
|
||
|
||
print(int("6"), type(int("6")))
|
||
```
|
||
|
||
**执行结果**:
|
||
```python
|
||
1.0 <class 'float'>
|
||
|
||
6 <class 'int'>
|
||
```
|
||
|
||
**使用int函数转换类型时,转换的内容一定是数字。**
|
||
|
||
**示例代码**:
|
||
```python
|
||
print(float("6"), type(float("6")))
|
||
|
||
print(float("a"), type(float("a")))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
6.0 <class 'float'>
|
||
|
||
ValueError: could not convert string to float: 'a'
|
||
```
|
||
|
||
### 字符串
|
||
> 使用 **str** 可以转换数据为字符串
|
||
|
||
**示例代码**:
|
||
```python
|
||
# 转换数字为字符串
|
||
print(str(1), type(str(1)))
|
||
# 转换列表为字符串
|
||
print(str([1,4,"a",0,"fj"]), type(str([1,4,"a",0,"fj"])))
|
||
# 转换字典为字符串
|
||
print(str({"a":1}), type(str({"a":1})))
|
||
```
|
||
|
||
**执行结果**:
|
||
```python
|
||
1 <class 'str'>
|
||
[1, 4, 'a', 0, 'fj'] <class 'str'>
|
||
{'a': 1} <class 'str'>
|
||
```
|
||
|
||
### 列表
|
||
> 使用 **list** 可以转换数据为列表
|
||
|
||
**示例代码**:
|
||
```python
|
||
# 字符串转换列表
|
||
print(list("abc"), type(list("abc")))
|
||
# 字典转换列表,转换时只转换key,而不会转换value,返回的是key列表
|
||
print(list({"a":1}), type(list({"a":1})))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
['a', 'b', 'c'] <class 'list'>
|
||
['a'] <class 'list'>
|
||
```
|
||
|
||
### 元组
|
||
> 使用 **tuple** 可以转换数据为元组
|
||
|
||
**示例代码**:
|
||
```python
|
||
# 字符串转元组
|
||
print(tuple("abc"), type(tuple("abc")))
|
||
# 字典转元组,与字典转列表相同,只转换key
|
||
print(tuple({"a":1}), type(tuple({"a":1})))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
('a', 'b', 'c') <class 'tuple'>
|
||
('a',) <class 'tuple'>
|
||
```
|
||
|
||
**列表和元组的转换方法,list/tuple,
|
||
在转换字符串时,
|
||
均为转换字符串中的每一个元素,
|
||
作为列表/元组中的每个元素**
|
||
|
||
### 字典
|
||
> 使用 **dict** 可以转换数据为字典
|
||
|
||
**示例代码**:
|
||
```python
|
||
print(dict([("a", 1)]), type(dict([("a", 1)])))
|
||
```
|
||
|
||
**执行结果**:
|
||
```python
|
||
{'a': 1} <class 'dict'>
|
||
```
|
||
**dict函数传参时,一定要按照一定的规范来传参数,否则执行会抛出异常**
|
||
|
||
**示例代码**:
|
||
```python
|
||
print(dict('{"a":1}'), type(dict('{"a":1}')))
|
||
```
|
||
|
||
**执行结果**:
|
||
```python
|
||
ValueError: dictionary update sequence element #0 has length 1; 2 is required
|
||
```
|
||
|
||
### 集合
|
||
> 使用 **set** 可以转换数据为集合
|
||
|
||
**示例代码**:
|
||
```python
|
||
# 列表转集合
|
||
print(set([1,2,4,7]), type(set([1,2,4,7])))
|
||
# 字符串转集合
|
||
print(set("1111"), type(set("1111")))
|
||
```
|
||
|
||
**执行结果**:
|
||
```python
|
||
{1, 2, 4, 7} <class 'set'>
|
||
{'1'} <class 'set'>
|
||
```
|
||
|
||
### 布尔类型
|
||
> 使用 **bool** 可以转换数据为布尔值
|
||
|
||
- 非0为True
|
||
- 非空为True
|
||
- None为False
|
||
|
||
**示例代码**:
|
||
```python
|
||
print(type(bool(10)), bool(10)) # 非0为True
|
||
print(type(bool(0)), bool(0))
|
||
print(type(bool(-3)), bool(-3))
|
||
print(type(bool("a")), bool("a")) # 非空为True
|
||
print(type(bool("")), bool(""))
|
||
print(type(bool(None)), bool(None)) # None为False
|
||
print(type(bool(True)), bool(True))
|
||
print(type(bool(False)), bool(False))
|
||
print(type(bool({})), bool({}))
|
||
print(type(bool({"a":1})), bool({"a":1}))
|
||
print(type(bool([])), bool([]))
|
||
print(type(bool([1])), bool([1]))
|
||
print(type(bool(())), bool(()))
|
||
print(type(bool((1))), bool((1)))
|
||
print(type(bool({1,3})), bool({1,3}))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
<class 'bool'> True
|
||
<class 'bool'> False
|
||
<class 'bool'> True
|
||
<class 'bool'> True
|
||
<class 'bool'> False
|
||
<class 'bool'> False
|
||
<class 'bool'> True
|
||
<class 'bool'> False
|
||
<class 'bool'> False
|
||
<class 'bool'> True
|
||
<class 'bool'> False
|
||
<class 'bool'> True
|
||
<class 'bool'> False
|
||
<class 'bool'> True
|
||
<class 'bool'> True
|
||
```
|
||
|
||
### eval()
|
||
> 使用 **eval** 用来计算在字符串中的有效Python表达式,并返回一个对象
|
||
|
||
**示例代码**:
|
||
```python
|
||
print(type(eval('{"a":1}')), eval('{"a":1}'))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
<class 'dict'> {'a': 1}
|
||
```
|
||
**示例代码**:
|
||
```python
|
||
print(type(eval('[2,4,"b","ooo"]')), eval('[2,4,"b","ooo"]'))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
<class 'list'> [2, 4, 'b', 'ooo']
|
||
```
|
||
**示例代码**:
|
||
```python
|
||
print(list('[2,4,"b","ooo"]'))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
['[', '2', ',', '4', ',', '"', 'b', '"', ',', '"', 'o', 'o', 'o', '"', ']']
|
||
```
|
||
**示例代码**:
|
||
```python
|
||
print(type(eval('[2,4,"b","ooo", True]')), eval('[2,4,"b","ooo", True]'))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
<class 'list'> [2, 4, 'b', 'ooo', True]
|
||
```
|
||
**示例代码**:
|
||
```python
|
||
print(type(eval('[2,4,"b","ooo", None]')), eval('[2,4,"b","ooo", None]'))
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
<class 'list'> [2, 4, 'b', 'ooo', None]
|
||
```
|
||
|
||
### join()
|
||
> 使用 **join** 元组转换字符串
|
||
|
||
正确代码:
|
||
```python
|
||
a = ",".join(["a","b","c","d"]) # a,b,c,d
|
||
```
|
||
|
||
异常代码:
|
||
```python
|
||
a = "".join([1,2,3,4,5,6,7,8,9,10])
|
||
```
|
||
**执行结果**:
|
||
```python
|
||
TypeError: sequence item 0: expected str instance, int found
|
||
```
|
||
|