1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
def
func(a, b):
return
a
*
b
class
MyClass:
def
__init__(
self
, a, b):
self
.a
=
a
self
.b
=
b
def
method(
self
):
return
self
.a
*
self
.b
导入模块:
>>>
import
test
>>> test.func(
2
,
2
)
4
>>> c
=
test.MyClass(
2
,
2
)
>>> c.method()
4
|
1
2
3
4
5
6
7
8
9
10
|
>>>
from
test
import
func, MyClass
# 多个函数或类以逗号分隔
>>> test.func(
2
,
2
)
Traceback (most recent call last):
File
"<stdin>"
, line
1
,
in
<module>
NameError: name
'test'
is
not
defined
>>> func(
2
,
2
)
4
>>> c
=
MyClass(
2
,
2
)
>>> c.method()
4
|
1
2
3
4
5
6
|
>>>
from
test
import
*
>>> func(
2
,
2
)
4
>>> c
=
MyClass(
2
,
2
)
>>> c.method()
4
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
def
func(a, b):
return
a
*
b
class
MyClass:
def
__init__(
self
, a, b):
self
.a
=
a
self
.b
=
b
def
method(
self
):
return
self
.a
*
self
.b
print
__name__
# python test.py
__main__
|
1
2
3
4
5
6
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
test
# python test2.py
test
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
def
func(a, b):
return
a
*
b
class
MyClass:
def
__init__(
self
, a, b):
self
.a
=
a
self
.b
=
b
def
method(
self
):
return
self
.a
*
self
.b
if
__name__
=
=
"__main__"
:
print
"我在手动执行这个程序..."
# python test.py
我在手动执行这个程序...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
方式
1
:
# wget https://pypi.python.org/packages/32/3c/e853a68b703f347f5ed86585c2dd2828a83252e1216c1201fa6f81270578/setuptools-26.1.1.tar.gz
# tar zxvf setuptools-26.1.1.tar.gz
# cd setuptools-26.1.1
# python setup.py install
方式
2
:
# easy_install setuptools
方式
3
:
# pip install setuptools
# pip uninstall setuptools # 卸载
# pip search setuptools # 搜索
方式
3
:
cp
-
rf setuptools
-
26.1
.
1
/
usr
/
local
/
lib
/
python2.
7
/
dist
-
packages
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
>>>
import
collections
>>>
help
(collections)
Help
on module collections:
NAME
collections
FILE
/
usr
/
lib
/
python2.
7
/
collections.py
MODULE DOCS
http:
/
/
docs.python.org
/
library
/
collections
# 注意:这里是这个模块的帮助文档,很详细的哦!
CLASSES
__builtin__.
dict
(__builtin__.
object
)
Counter
OrderedDict
defaultdict
__builtin__.
object
_abcoll.
Callable
_abcoll.Container
......
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
>>>
help
(collections.Counter())
Help
on Counter
in
module collections
object
:
class
Counter(__builtin__.
dict
)
|
Dict
subclass
for
counting hashable items. Sometimes called a bag
|
or
multiset. Elements are stored as dictionary keys
and
their counts
| are stored as dictionary values.
|
| >>> c
=
Counter(
'abcdeabcdabcaba'
)
# count elements from a string
|
| >>> c.most_common(
3
)
# three most common elements
| [(
'a'
,
5
), (
'b'
,
4
), (
'c'
,
3
)]
| >>>
sorted
(c)
# list all unique elements
| [
'a'
,
'b'
,
'c'
,
'd'
,
'e'
]
| >>> ''.join(
sorted
(c.elements()))
# list elements with repetitions
|
'aaaaabbbbcccdde'
| >>>
sum
(c.values())
# total of all counts
|
15
|
| >>> c[
'a'
]
# count of letter 'a'
......
|
1
2
|
>>>
dir
(collections)
[
'Callable'
,
'Container'
,
'Counter'
,
'Hashable'
,
'ItemsView'
,
'Iterable'
,
'Iterator'
,
'KeysView'
,
'Mapping'
,
'MappingView'
,
'MutableMapping'
,
'MutableSequence'
,
'MutableSet'
,
'OrderedDict'
,
'Sequence'
,
'Set'
,
'Sized'
,
'ValuesView'
,
'__all__'
,
'__builtins__'
,
'__doc__'
,
'__file__'
,
'__name__'
,
'__package__'
,
'_abcoll'
,
'_chain'
,
'_class_template'
,
'_eq'
,
'_field_template'
,
'_get_ident'
,
'_heapq'
,
'_imap'
,
'_iskeyword'
,
'_itemgetter'
,
'_repeat'
,
'_repr_template'
,
'_starmap'
,
'_sys'
,
'defaultdict'
,
'deque'
,
'namedtuple'
]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
collections
c
=
collections.Counter()
for
i
in
"Hello world!"
:
c[i]
+
=
1
print
c
# python collections.py
Traceback (most recent call last):
File
"collections.py"
, line
3
,
in
<module>
import
collections
File
"/home/user/collections.py"
, line
4
,
in
<module>
c
=
collections.Counter()
AttributeError:
'module'
object
has no attribute
'Counter'
|
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
import
collections as cc
c
=
cc.Counter()
for
i
in
"Hello world!"
:
c[i]
+
=
1
print
c
# python collections1.py
Counter({
'l'
:
3
,
'o'
:
2
,
'!'
:
1
,
' '
:
1
,
'e'
:
1
,
'd'
:
1
,
'H'
:
1
,
'r'
:
1
,
'w'
:
1
})
|