python语法——数据结构(代码)
来源:互联网 发布:淘宝买衣服怎么买 编辑:程序博客网 时间:2024/06/07 02:43
源网页:http://woodpecker.org.cn/abyteofpython_cn/chinese/ch09s02.html
列表
#!/usr/bin/python
# Filename: using_list.py
# This is my shopping listshoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
print
'I have'
,
len
(shoplist),
'items to purchase.'
print
'These items are:'
,
# Notice the comma at end of the line
for
item
in
shoplist:
print
item,
print
'\nI also have to buy rice.'
shoplist.append(
'rice'
)
print
'My shopping list is now'
, shoplist
print
'I will sort my list now'
shoplist.sort()
print
'Sorted shopping list is'
, shoplist
print
'The first item I will buy is'
, shoplist[
0
]
olditem = shoplist[
0
]
del
shoplist[
0
]
print
'I bought the'
, olditem
print
'My shopping list is now'
, shoplist
输出
$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
元组
#!/usr/bin/python
# Filename: using_tuple.pyzoo = (
'wolf'
,
'elephant'
,
'penguin'
)
print
'Number of animals in the zoo is'
,
len
(zoo)
new_zoo = (
'monkey'
,
'dolphin'
, zoo)
print
'Number of animals in the new zoo is'
,
len
(new_zoo)
print
'All animals in new zoo are'
, new_zoo
print
'Animals brought from old zoo are'
, new_zoo[
2
]
print
'Last animal brought from old zoo is'
, new_zoo[
2
][
2
]
使用
new_zoo[2][2]
来访问new_zoo
元组的第三个项目的第三个项目。
$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
字典
#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ookab = {
'Swaroop'
:
'swaroopch@byteofpython.info'
,
'Larry'
:
'larry@wall.org'
,
'Matsumoto'
:
'matz@ruby-lang.org'
,
'Spammer'
:
'spammer@hotmail.com'
}
print
"Swaroop's address is %s"
% ab[
'Swaroop'
]
# Adding a key/value pair
ab[
'Guido'
] =
'guido@python.org'
# Deleting a key/value pair
del
ab[
'Spammer'
]
print
'\nThere are %d contacts in the address-book\n'
%
len
(ab)
for
name, address
in
ab.items():
print
'Contact %s at %s'
% (name, address)
if
'Guido'
in
ab:
# OR ab.has_key('Guido')
print
"\nGuido's address is %s"
% ab[
'Guido'
]
$ python using_dict.py
Swaroop's address is swaroopch@byteofpython.info
There are 4 contacts in the address-book
Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org
Guido's address is guido@python.org
序列
#!/usr/bin/python
# Filename: seq.pyshoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
# Indexing or 'Subscription' operation
print
'Item 0 is'
, shoplist[
0
]
print
'Item 1 is'
, shoplist[
1
]
print
'Item 2 is'
, shoplist[
2
]
print
'Item 3 is'
, shoplist[
3
]
print
'Item -1 is'
, shoplist[
-1
]
print
'Item -2 is'
, shoplist[
-2
]
# Slicing on a list
print
'Item 1 to 3 is'
, shoplist[
1
:
3
]
print
'Item 2 to end is'
, shoplist[
2
:
]
print
'Item 1 to -1 is'
, shoplist[
1
:
-1
]
print
'Item start to end is'
, shoplist[:]
# Slicing on a string
name =
'swaroop'
print
'characters 1 to 3 is'
, name[
1
:
3
]
print
'characters 2 to end is'
, name[
2
:]
print
'characters 1 to -1 is'
, name[
1
:
-1
]
print
'characters start to end is'
, name[:]
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
对象与参考
#!/usr/bin/python
# Filename: reference.pyprint
'Simple Assignment'
shoplist = [
'apple'
,
'mango'
,
'carrot'
,
'banana'
]
mylist = shoplist
# mylist is just another name pointing to the same object!
del
shoplist[
0
]
print
'shoplist is'
, shoplist
print
'mylist is'
, mylist
# notice that both shoplist and mylist both print the same list without
# the 'apple' confirming that they point to the same objectprint
'Copy by making a full slice'
mylist = shoplist[:]
# make a copy by doing a full slice
del
mylist[
0
]
# remove first item
print
'shoplist is'
, shoplist
print
'mylist is'
, mylist
# notice that now the two lists are different
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
字符串
name =
'Swaroop'
# This is a string object
if
name.startswith(
'Swa'
):
print
'Yes, the string starts with "Swa"'
if
'a'
in
name:
print
'Yes, it contains the string "a"'
if
name.find(
'war'
) !=
-1
:
print
'Yes, it contains the string "war"'
delimiter =
'_*_'
mylist = [
'Brazil'
,
'Russia'
,
'India'
,
'China'
]
print
delimiter.join(mylist)
输出
$ python str_methods.py
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
- python语法——数据结构(代码)
- 代码Python入门(一、基础语法)
- python基础语法2(数据结构)
- python语法学习之数据结构
- python—基本语法
- Python 的数据结构——序列讲解(附实例代码)
- Verilog语法基础(2)——数据结构1
- Verilog语法基础(2)——数据结构2
- Python —— 数据结构
- Python数据结构—-栈
- Python进阶(三)——Python语法
- Python进阶(三)——Python语法
- [Python]python学习笔记(一)——语法
- Python进阶(三)——Python语法
- Python初学(5)——Python语法规则
- Python进阶(三)——Python语法
- 一段python代码 熟悉语法
- Python基础——语法
- 探寻Android系统中的Old CRT OFF-Screen Animation
- 横屏时设置输入法不挡入输入框的方法
- SQL SERVER使用存储过程 清除表数据
- MAKEFILE简明指南
- Solder Mask与Paste Mask
- python语法——数据结构(代码)
- opencv 图像帧差法(图像相减) 代码
- Hadoop传递参数的方法总结
- java.lang.NoClassDefFoundError:javax/persistence/Cacheable
- DESUtil.java
- VxWorks引导启动过程
- JavaScript笔记——事件的类型
- ssh url
- Linux 内核剖析:历史和体系结构分析