Python学习笔记——基本数据结构
列表list
List是python的一个内置动态数组对象,它的基本使用方式如下:
shoplist = ['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
元组Tuple
Python的typle通过小括号初始化,是一个只读对象。不过它的成员具有数组的访问方式。
zoo = ('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]
字典dict
Dict是一个查询式的数据结构,它的基本用法如下:
ab = { '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学习笔记——基本数据结构的更多相关文章
- python学习笔记五——数据结构
4 . python的数据结构 数据结构是用来存储数据的逻辑结构,合理使用数据结构才能编写出优秀的代码.python提供的几种内置数据结构——元组.列表.字典和序列.内置数据结构是Python语言的精 ...
- Python学习笔记系列——数据结构相关
Python有4种数据结构:列表(list).字典(dictionary).元组(Tuple).集合(set).从最直接的感官上来说,这四种数据结构的区别是:列表中的元素使用方括号括起来,字典和集合是 ...
- Python学习笔记(3)--数据结构之列表list
Python的数据结构有三种:列表.元组和字典 列表(list) 定义:list是处理一组有序项目的数据结构,是可变的数据结构. 初始化:[], [1, 3, 7], ['a', 'c'], [1, ...
- Python学习笔记(5)--数据结构之字典dict
字典(dict) 定义:键值对集合 初始化:{}, {'1' : 'abc', '2' : 'def'} 1.增加:单个数据直接赋值 update(dict2) ---把dict2的元素加入到dic ...
- Python学习笔记(4)--数据结构之元组tuple
元组(tuple) 定义:tuple和list十分相似,但是tuple是不可变的,即不能修改tuple 初始化:(), ('a', ) , ('a', 'b') //当只有一个元素时,需加上逗号, ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- python学习笔记整理——元组tuple
Python 文档学习笔记2 数据结构--元组和序列 元组 元组在输出时总是有括号的 元组输入时可能没有括号 元组是不可变的 通过分拆(参阅本节后面的内容)或索引访问(如果是namedtuples,甚 ...
- python学习笔记之module && package
个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...
- Python学习笔记,day5
Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...
随机推荐
- css background-size与背景图片填满div
background-size与背景图片填满div 在开发中,常有需要将一张图片作为一个div的背景图片充满div的需求 background-size的取值及解释 background-size共有 ...
- Python-map、filter、reduce方法
介绍 1.map()函数,会让列表中每一个元素都执行一某个函数(传递1个参数), 并且将执行函数返回的结果(无论是什么结果)放在结果列表中 2.filter()函数,会让列表中的每一个元素都执行一次某 ...
- UVa 11806 - Cheerleaders (组合计数+容斥原理)
<训练指南>p.108 #include <cstdio> #include <cstring> #include <cstdlib> using na ...
- 软工实践 - 第十三次作业 Alpha 冲刺 (4/10)
队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/9980005.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去 ...
- ArcGIS 创建要素时提示“表已经被注册(table already registered)”
今天一位实施大哥在ArcCatalog中要重建一个要素类,所以就在ArcCatalog中连接上Oracle数据库,直接删除了要素类,然后重新创建了一个新的要素类,可是却报错“表已经被创建”,并提示不可 ...
- vADC-KVM and vADC-ESX.ovf
vADC-KVM and vADC-ESX.ovf vADC-ESX.ovf.xml <?xml version="1.0" encoding="UTF-8&q ...
- [洛谷P1972][SDOI2009]HH的项链
题目大意:给你一串数字,多次询问区间内数字的种类数 题解:莫队 卡点:洛谷数据加强,开了个$O(2)$ C++ Code: #include <cstdio> #include <a ...
- <转自原博客> 可爱的字符串算法们
在非常强又非常关心学弟学妹学习的企鹅学长变态的考纲下,我们无奈中选择一起学习新姿势 first:KMP算法 这是一个小迪更过博客的算法,我就不好意思在这里献丑了,所以献上友链一份:http://rab ...
- POJ 2406 Power Strings 暴力
emmmm 显然的是a串长度是s串长度的因数 我们可以暴力枚举因数然后暴力check #include<cstdio> #include<algorithm> #include ...
- sql优化 in 和 not in 语句
WHY? IN 和 NOT IN 是比较常用的关键字,为什么要尽量避免呢? 1.效率低 可以参看我之前遇到的一个例子([小问题笔记(九)] SQL语句Not IN 效率低,用 NOT EXISTS试试 ...