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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#coding:utf-8
#字典 列表字典生成式
#字符串 字符串转换
#文件操作 文件字典转换 用户登录注册 系统
shoplist
=
[
'apple'
,
'mango'
,
'banana'
,
'carrot'
]
#print shoplist.count('apple')
count
=
0
for
i
in
shoplist:
if
i
=
=
'apple'
:
count
+
=
1
print
count
#列表去重
a
=
[
1
,
2
,
3
,
4
,
1
,
3
,
5
]
print
set
(a)
#函数去重
def
quchong():
a
=
[
'apple'
,
'mango'
,
'banana'
,
'carrot'
,
'apple'
]
new
=
[]
for
i
in
a:
if
not
i
in
new:
new.append(i)
return
new
print
quchong()
#将主机192.168.1.1-254存入列表
host_list
=
[]
netip
=
"192.168.1."
for
hostip
in
range
(
1
,
254
):
ip
=
netip
+
str
(hostip)
host_list.append(ip)
print
host_list
#遍历
for
index,value
in
enumerate
(shoplist):
print
index,value
#列表生成式
#格式[x for x in 内容] [x for x in 内容 if 条件]
fields
=
[
'a'
,
'b'
,
'c'
]
data
=
{
'a'
:
'abc'
,
'b'
:
'bac'
,
'c'
:
'cba'
}
print
[data[x]
for
x
in
fields]
a
=
[]
for
i
in
fields:
a.append(data[i])
print
a
b
=
[]
for
k,v
in
data.items():
b.append(v)
print
b
print
[v
for
k,v
in
data.items()]
print
','
.join([data[x]
for
x
in
fields])
print
','
.join(
'"%s"'
%
data[x]
for
x
in
fields )
#字典 无序
content
=
{
'name'
:
'wd'
,
'pc'
:{
'age'
:
18
,
'iphone'
:
'111'
},
'woniu'
:[
1
,
2
,
3
]}
print
content.keys()
print
content.values()
print
content.items()
content[
'test'
]
=
111222333
content.pop(
"test"
)
content[
'pc'
][
'age'
]
=
20
content[
'woniu'
][
0
]
=
444
print
content.get(
"name"
,
"dasdasdas"
)
print
content
res
=
content.get(
"name"
,"")
if
res:
print
"ok"
else
:
print
"fail"
print
content.has_key(
"name"
)
if
"name"
in
content:
print
"key is exist"
else
:
print
"this is key is no exist"
for
k,v
in
content.items():
print
k,v
#字典应用
for
k,v
in
content.items():
print
k
+
":"
if
type
(v)
=
=
dict
:
for
x,y
in
v.items():
print
x,y
elif
type
(v)
=
=
list
:
for
z
in
v:
print
z
else
:
print
v
strcontent
=
"who are you My name is fujinzhou and you"
#字符串取代噢空格
print
strcontent.split(
" "
)
dicts
=
{}
arr
=
strcontent.split(
' '
)
for
i
in
arr:
if
i
not
in
dicts:
dicts[i]
=
1
else
:
dicts[i]
+
=
1
print
dicts
#因为很多单词出现了一次,一旦k,v反转, 1作为key ,这个key 会被一只覆盖到最后一个
#方案:把出现的数字作为key,所有出现次数一样的单词保存为列表
reslist
=
{}
for
k,v
in
dicts.items():
reslist.setdefault(v,[])
reslist[v].append(k)
print
reslist
count
=
0
f
=
open
(
'count.html'
,
'a+'
)
f.write(
"<html><table style='border:solid 1px'>"
)
f.write(
"<th style='border:solid 1px'>次数</th><th style='border:solid 1px'>单词</th>"
)
while
count <
4
:
key
=
max
(reslist.keys())
print
key
print
"出现了%s次的单词:%s"
%
(key,reslist[key])
for
word
in
reslist[key]:
f.write(
'<tr><td style="border:solid 1px">%s</td><td style="border:solid 1px">%s</td></tr>'
%
(key,word))
reslist.pop(key)
count
=
count
+
1
f.write(
"</table></html>"
)
f.close()
|
本文转自 shouhou2581314 51CTO博客,原文链接:xxxhttp://blog.51cto.com/thedream/1829710xxxx,如需转载请自行联系原作者