# 二维列表
list1 =[]for i inrange(2):
list1.append([])for j inrange(3,10,2):
list1[i].append(j)print(list1)print(list1[1][1])
[[3, 5, 7, 9], [3, 5, 7, 9]]
5
# 连接字符串:
str1 ="we are a warm"
str2 ="family"print(str1 +" "+ str2)print()
str3 =[str1,str2]print(' '.join(str3))
we are a warm family
we are a warm family
help(str.split)
Help on method_descriptor:
split(self, /, sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string.
sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
# 拆分字符串到一个列表
str1.split(sep =" ")
['we', 'are', 'a', 'warm']
string1 ="believe,lie,lie"
string1.count("lie")
3
string1.find("lie")
2
names =["磊","亚","涛","虎","光","敏","海","丹"]
操作列表
for name in names:print(name)
磊
亚
涛
虎
光
敏
海
丹
help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
for name in names:print(name, end =" ")
磊 亚 涛 虎 光 敏 海 丹
names.index("丹")
7
for index,name inenumerate(names):print(index,name)
0 磊
1 亚
2 涛
3 虎
4 光
5 敏
6 海
7 丹
for i inrange(5):print(i,end =' ')print()for j inrange(1,4):print(j,end =' ')print()for m inrange(1,10,2):print(m,end =' ')
0 1 2 3 4
1 2 3
1 3 5 7 9
list2 =[x for x inrange(9)]
list2
[0, 1, 2, 3, 4, 5, 6, 7, 8]
max(list2)
8
min(list2)
0
list2.count(8)
1
from collections import Counter
dict(Counter(list2))