-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalist.py
More file actions
55 lines (42 loc) · 1.19 KB
/
alist.py
File metadata and controls
55 lines (42 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author:jiaming
# Date: 2018/12/19 22:22
# Describe: DataType -> List
# 定义和赋值
list1 = ['a', 'b', 'c']
list2 = []
print(list1)
nums = range(1, 10)
print(nums)
for x in nums:
print(x)
# python list 可以存放不同类型的数据
list3 = [1, 2, 'ab', True, ['a', 'b', 'c']]
print(list3)
# 列式推导
resultList = []
for num in nums:
resultList.append(num ** 2)
print('resultList =', resultList)
# [表达式 for 变量 in 列表]
resultList1 = [num ** 2 for num in nums]
print('resultList1 =', resultList1)
# [表达式 for 变量 in 列表 if 表达式]
resultList2 = [num ** 2 for num in nums if num % 2 != 0]
print('resultList2 =', resultList2)
# len()
print('len(list1) =', len(list1))
# 将一个元素添加到列表结尾 list.append(x)
# 会修改原数组
list1.append(11)
print('list1 =', list1)
# list1[len(list1):] = 12 TypeError: can only assign an iterable
list1[len(list1):] = [12]
print('list1 =', list1)
# 将特定列表的元素添加到另一个列表中 list.extend(L) 同list[len(list):] = L
list3 = ['a', 'b', 'c']
list1.extend(list3)
print('list1 =', list1)
list1[len(list1):] = [13, 14]
print('list1 =', list1)