Python4:DataStructure_pydatastructure-程序员宅基地

技术标签: Python通关之路  

1.Introduction

数据结构基本上就是可以处理一些数据的结构。或者说,它们是用来存储一组相关数据的。在Python中有三种内建的数据结构——列表、元组和字典。

2.列表

list是处理一组有序项目的数据结构,即可以在一个列表中存储一个序列的项目。假想有一个购物列表,上面记载着要买的东西,就容易理解列表了。只不过在你的购物表上,可能每样东西都独自占有一行,而在Python中,在每个项目之间用逗号分割。
列表中的项目应该包括在方括号中,这样Python就知道你是在指明一个列表。一旦创建了一个列表,就可以添加、删除或是搜索列表中的项目。由于可以增加或删除项目,我们说列表是可变的数据类型,即这种类型是可以被改变的。
#using_list.py
#This is my shopping list
shoplist =['apple','mango','carrot','banana'];
print 'i have', len(shoplist), 'items to purchase';

print 'There items are :';
for item in shoplist:
    print item;

print '\n I 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;

3.元组

元组和列表十分类似,只不过元组和字符串一样是不可变的,即不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
#using_tuple.py
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 the 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];

4.字典

字典类似于通过联系人名字查找地址和联系人详细情况的地址簿,即,把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的。
键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒
号分割,而各个对用逗号分割,所有这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果想要一个特定的顺序,那么应该在使用前对它们排序。字典是dict类的实例/对象。
#using_dict.py
#ab = address book
ab = {'Swaroop'  :'[email protected]',
      'Larry'    :'[email protected]',
      'Matsumoto':'[email protected]',
      'Spammer'  :'[email protected]'
      };
print "Swaroop's address is %s :", ab['Swaroop'];
#adding a key/value pair
ab['Guido'] = '[email protected]';

#deleting 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:
    print "\nGuido's address is %s" % ab['Guido'];

5.序列

列表、元组和字符串都是序列。序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。
#using_sqe.py
shoplist = ['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-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-end is', name[:]

6.reference

当创建一个对象并给它赋一个变量的时候,这个变量仅仅引用那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这称作名称到对象的绑定。一般说来,不需要担心这个,只是在引用上有些细微的效果需要注意。示例如下:
#using_reference.py
print '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 object
print '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

7.String

# String_method.py
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)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/shenziheng1/article/details/77014602

智能推荐

二阶常微分方程(ODE)的打靶法(Shooting method),有限差分基础(python)_二阶常系数非齐次微分方程打靶法-程序员宅基地

文章浏览阅读1.3w次,点赞17次,收藏91次。第四十九篇 二阶常微分方程的打靶法边界值问题当我们试图用自变量的不同值所提供的信息来解二阶或更高阶的常微分方程时,我们必须使用与前面描述的不同的数值方法。前面提到的初值问题经常涉及到“时间”作为自变量,解决技术要求我们按步骤“前进”,直到达到所需的解。在这种情况下,解的定义域不是有限的,因为原则上我们可以无限地沿着正方向或负方向前进。边值问题涉及一个有限解的区间,在这个区间内去求解。这类问题的自变量通常是空间中测量距离的坐标。一个典型的二阶边值问题可能下面的形式。解的定义域(假设为B >a_二阶常系数非齐次微分方程打靶法

Nacos微服务注册地址为内网IP的解决办法_mapp.clientregisterip nacos-程序员宅基地

文章浏览阅读6.3w次,点赞16次,收藏61次。各个服务通过Nacos客户端将服务信息注册到Nacos上当Nacos服务注册的IP默认选择出问题时,可以通过查阅对应的客户端文档,来选择配置不同的网卡或者IP例如,使用了Spring cloud alibaba作为Nacos客户端,服务默认获取了内网IP `192.168.1.21`,可以通过配置`spring.cloud.inetutils.preferred-networks=10.34.12 `,使服务获取内网中前缀为`10.34.12`的IP_mapp.clientregisterip nacos

第四次工业革命将来自人工智能和物联网。-程序员宅基地

文章浏览阅读385次。毫不奇怪,“关于一切的信息”的概念正在积极地应用于制造业背景。就像他们改变消费品一样,智能,廉价,传感器负载的设备与强大的分析和算法相结合,在过去十年中也在改变工业世界。“物联网”已经到达工厂车间,一个巨大的电子Kool-Aid Man的力量通过煤渣墙爆炸。大数据的分析和机器学习开始变得像匿名商业词汇,但它们不仅仅是过度使用抽象概念 - 这些流行语代表了我们日常生活中处理大部分技术的巨大变化。...

new Promise请求后台的数据返回成功以后,在去执行操作其他操作_new promise 里面包含请求后端接口-程序员宅基地

文章浏览阅读1.7k次,点赞2次,收藏3次。//记录下,如果在一个函数需要保证,请求后台的数据返回成功以后,在去执行操作其他操作//addEnv是axios请求以后封装好的接口function cat(){ return new Promise((reslove,reject)=>{ addEnv({id:1}).then(res=>{ reslove(res) }).catch(error=>{ reject(error) }) })}async function sheep(){ let r_new promise 里面包含请求后端接口

webstrom 的安装-程序员宅基地

文章浏览阅读253次。webstorm是一个前端开发神器。安装webstorm之前需要配置jdk。配置好jdk之后再进行安装webstorm;1.下载链接jdk:http://download.oracle.com/otn-pub/java/jdk/8u73-b02/jdk-8u73-linux-i586.tar.gz?AuthParam=1472692770_2276e0b95c38424d5105a4357..._webstorm需要配置jdk吗

数据结构——平衡二叉树的判断【递归算法】(C语言)_balance treec语言-程序员宅基地

文章浏览阅读874次。平衡二叉树的判断(左右子树的高度差只能为-1,0,1)#include<stdio.h>#include<stdlib.h>#include<queue>#include <iostream>#define MAXSIZE 10010#define ElemType intusing namespace std;typedef struct BTNode{ ElemType data; BTNode *lchild,*._balance treec语言

随便推点

Android ADB命令大全_android adb kill service-程序员宅基地

文章浏览阅读2w次,点赞9次,收藏89次。adb的全称为Android Debug Bridge.是android司机经常用到的工具.但是问题是那么多命令写代码已经够费劲了,过段时间在次使用时压根记不住呀.本次的大餐就是为此开篇的.这一次我们不记命令.要用随时过来ctrl+F呀.哇哈哈哈!本篇ADB集锦不管是常用还是冷门的都有.客观您随意看.你能在本篇文章中收获什么?_android adb kill service

IntelliJ IDEA中Tomcat服务器中无Update classes and resources的解决方法_idea 没有update classes and resources-程序员宅基地

文章浏览阅读3.9k次,点赞11次,收藏12次。1.问题描述第一次使用ssm框架发现on “Update” action中没有Update classes and resources选项了,这就是说如果更新静态资源都要重启服务器,在开发过程中十分麻烦,查了一晚上也没有找到bug2.解决办法原来是artifact的问题:Artifacts,它的作用是整合编译后的 java 文件,资源文件等,有不同的整合方式,比如war、jar、war exploded 等,对于 Module 而言,有了 Artifact 就可以部署到 web 容器中了。其中 w_idea 没有update classes and resources

iPhone白苹果-程序员宅基地

文章浏览阅读73次。iPhone遇到了白苹果问题如何解决?

OPPO面试题 笔试题 搜集_oppo嵌入式笔试-程序员宅基地

文章浏览阅读4.2k次。1、面试在校期间,学得最好的专业课程是什么,问课程相关知识C语言内存结构和struct内存对齐最能体现编程功底的项目,项目难点,io操作应该不少吧,io、多线程了解吗?2、笔试3、HR面试学习了哪些课外知识,怎么学习的,学到了什么你觉得你的核心竞争力是什么?为什么把他作为核心竞争力?除了这个,你觉得还有什么竞争力..._oppo嵌入式笔试

Hibernate缓存机制-程序员宅基地

文章浏览阅读712次。缓存是位于应用程序与物理数据源之间,用于临时存放复制数据的内存区域,目的是为了减少应用程序对物理数据源访问的次数,从而提高应用程序的运行性能. Hibernate在查询数据时,首先到缓存中去查找,如果找到就直接使用,找不到的时候就会从物理数据源中检索,所以,把频繁使用的数据加载到缓存区后,就可以大大减少应用程序对物理数据源的访问,使得程序的运行性能明显的提升. Hibernate缓存分类: Session缓存,一级缓

项目使用nacos报错 com.alibaba.nacos.api.exception.NacosException: Param ‘beat‘ is required._com.alibaba.nacos.client.naming.net.namingproxy.ca-程序员宅基地

文章浏览阅读1w次,点赞2次,收藏2次。完整报错:com.alibaba.nacos.api.exception.NacosException: Param 'beat' is required. at com.alibaba.nacos.client.naming.net.NamingProxy.callServer(NamingProxy.java:611) [nacos-client-1.3.3.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.req..._com.alibaba.nacos.client.naming.net.namingproxy.callserver(namingproxy.java: