当前位置: 首页 > 编程日记 > 正文

python常用数据结构的常用操作

作为基础练习吧。列表LIST,元组TUPLE,集合SET,字符串STRING等等,显示,增删,合并。。。

复制代码
#===========List=====================
shoplist = ['apple','mango','carrot','banana']
print 'I have ',len(shoplist), ' items to purchase.'
print 'These items are:'
for item in shoplist:print item,
print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplistprint 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplistprint '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 ',shoplist
#=======================Tuple===================
zoo = ('python','elephant','penguin')
print 'Number of animals in the zoo is',len(zoo)new_zoo = 'monkey', 'camel', zoo
print 'Number of cages 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]
print 'Number of animal in the new zoo is ',\len(new_zoo)-1+len(new_zoo[2])
#===========Dictionary======================
ab = {'Swaroop' :'swaroop@swaroopch.com','Larry'   :'larry@wall.org','Matsumoto':'matz@ruby-lang.org','Spammer' :'spammer@hotmail.com'}
print "Swaroop's address is", ab['Swaroop']del ab['Spammer']print '\nThere are {0} contacts in the address-book.\n'.format(len(ab))
for name, address in ab.items():print 'Contact {0} at {1}'.format(name, address)ab['Guido'] = 'guido@python.org'if 'Guido' in ab:print "\nGuido's address is",ab['Guido']#=============Sequence==============
print 'Item 0 is',shoplist[0]
print 'Item 3 is',shoplist[3]
print 'Item -2 is',shoplist[-2]print 'Item 1 to 3 is',shoplist[1:3]
print 'Item start to end is',shoplist[:]
print 'Item step 2 is',shoplist[::2]
#=============Set================
bri = set(['brazil','russia','china','india'])
print 'india' in bri
print 'usa' in bri
bric = bri.copy()
bric.add('france')
print bric.issuperset(bri)
bri.remove('russia')
print bri & bric
#=================References================
print 'Simple Assignment'
mylist = shoplist
del shoplist[0]print 'shoplist is', shoplist
print 'mylist is', mylistprint 'Copy by making a full slice'
mylist = shoplist[:]
del mylist[0]print 'shoplist is', shoplist
print 'mylist is', mylist
#=====================String================
name = 'Swaroop'if name.startswith('Swa'):print 'Yes, the string starts with Swa'
if 'a' in name:print 'Yes, the string contains the string "a"'
if name.find('war') != -1:print 'Yes, the string contains the string "war"'
delimiter = '_*_'
mylist = ['BRAZIL','RUSSIA','INDIA','CHINA']
print delimiter.join(mylist)
复制代码

输出:

C:\webpy\webpy\Scripts\python.exe C:/pycode/pycode.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  ['banana', 'carrot', 'mango', 'rice']
Number of animals in the zoo is 3
Number of cages in the new zoo is 3
All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
Animals brought from old zoo are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animal in the new zoo is  5
Swaroop's address is swaroop@swaroopch.com

There are 3 contacts in the address-book.

Contact Swaroop at swaroop@swaroopch.com
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org

Guido's address is guido@python.org
Item 0 is banana
Item 3 is rice
Item -2 is mango
Item 1 to 3 is ['carrot', 'mango']
Item start to end is ['banana', 'carrot', 'mango', 'rice']
Item step 2 is ['banana', 'mango']
True
False
True
set(['brazil', 'china', 'india'])
Simple Assignment
shoplist is ['carrot', 'mango', 'rice']
mylist is ['carrot', 'mango', 'rice']
Copy by making a full slice
shoplist is ['carrot', 'mango', 'rice']
mylist is ['mango', 'rice']
Yes, the string starts with Swa
Yes, the string contains the string "a"
Yes, the string contains the string "war"
BRAZIL_*_RUSSIA_*_INDIA_*_CHINA

Process finished with exit code 0

相关文章:

h5 和native 交互那些事儿

前端菜菜一枚,写下关于h5 和native 交互那些事情。偏前端,各种理论知识,不在赘述。之前有各位大牛已经写过。我只写代码,有问题,下面留言/* 关于h5 和native 之间的交互 JSBridge 解决问题,偏向前端* 使用U…

手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫

系列教程 手把手教你写电商爬虫-第一课 找个软柿子捏捏 如果没有看过第一课的朋友,请先移步第一课,第一课讲了一些基础性的东西,通过软柿子"切糕王子"这个电商网站好好的练了一次手,相信大家都应该对写爬虫的流程有了一…

Python程序设计 第六章 函数(续

复习 1. 10进制 ⇒\Rightarrow⇒ 2进制 除2取余,从低位到高位存储到字符串中,从高位到低位def d2b(n):if n>1:d2b(n//2)print(n%2,end)d2b(4)出口: 条件,值确定 (一)return (二)函数体执行结…

K8S的横向自动扩容的功能Horizontal Pod Autoscaling

K8S 作为一个集群式的管理软件,自动化、智能化是免不了的功能。Google 在 K8S v1.1 版本中就加入了这个 Pod 横向自动扩容的功能(Horizontal Pod Autoscaling,简称 HPA)。 HPA 与之前的 Deployment、Service 一样,也属…

第八周例行报告

此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2326 1、本周PSP 类型 任务 开始时间 结束时间 中断时间 Delta时间 会议 事后诸葛亮会议 11.3 14:12 11.3 15:08 0min 56min 博客 编写博客《事后诸葛…

HTTP头部信息解释分析(详细整理)

这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型&#…

深圳杯---深圳市生活垃圾处理社会总成本分析

2017年3月18日,国务院向全国发布了《生活垃圾分类制度实施方案》,这标志着中国垃圾分类制度建设开始了一个全新阶段,垃圾分类已成为推进社会经济绿色发展、提升城市管理和服务水平、优化人居环境的重要举措。为了保证这一目标能够顺利实现&am…

你真的掌握了并发编程volatile synchronized么?

先看代码: import java.util.concurrent.atomic.AtomicInteger;/**** author xialuomantian*/ public class NewTest {static volatile int a 1;static volatile int b 1;//static int a 1;//static int b 1;public static AtomicInteger aa new AtomicInteg…

SQLSERVER存储过程基本语法使用

一、定义变量 --简单赋值 declare a int set a5 print a --使用select语句赋值 declare user1 nvarchar(50) select user1张三 print user1 declare user2 nvarchar(50) select user2 Name from ST_User where ID1 print user2 --使用update语句赋值 declare user3 nv…

线上java JVM问题排查

作者:霞落满天 第一部分 是我以前公司的一则正式案例: 第二部分 是我另一个博客上写的主要是最近发现大家问的比较多就写了此文 第一部分 线上真实故障案例 下面是一个老系统,代码写的有点问题导致出现这样一个JVM占比过高的问题&#xff…

走向云时代的大型机

大型机,又称大型主机,英文名mainframe,是指使用专用的处理器指令集、操作系统和应用软件的有机整体。大型机最早诞生于上个世纪六十年代,经过四十多年的不断发展,其在可靠性、安全性、可用性和灵活性方面首屈一指。近年…

区分 欧几里得距离 曼哈坦距离 明考斯基距离

欧几里德距离(Euclidean Distance),欧氏距离。一种通常采用的表示相似度的距离定义,是表示在m维空间中两个点之间的真实距离。 对于n维空间中的两个点之间的欧几里得距离d(i,j)表示为: d(i,j) (|xi1-xj1|2|xi2-xj2|2……|xip-xjp|2)1/2 当n2…

传统行业转型微服务的挖坑与填坑

原文:传统行业转型微服务的挖坑与填坑一、微服务落地是一个复杂问题,牵扯到IT架构,应用架构,组织架构多个方面 在多家传统行业的企业走访和落地了微服务之后,发现落地微服务是一个非常复杂的问题,甚至都不完全是技术问…

Windows下安装Mongodb SpringBoot集成MongoDB和Redis多数据源

全文内容: Mongodb安装 说明:Mongodb和redis是开发中常用的中间件,Redis的安装使用比较简单就不写了,只说本地也就是Windows安装Mongodb。 SpringBoot集成MongoDB和Redis 文中还有一个彩蛋Hutool 1.下载最新稳定版 https://w…

使用CSDN-markdown编辑器

欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰…

HTTP缓存相关头

本文说的是HTTP中控制客户端缓存的头有哪些。网上这方面的文章很多了,这里就说下个人的理解。 在请求一个静态文件的时候(图片,css,js)等,这些文件的特点是文件不经常变化,将这些不经常变化的文…

Thrift RPC 系列教程(4)——源码目录结构组织

Thrift 代码就是编程代码。是代码,就应该有良好的工程组织,并且,单独git仓库、版本管理,都是必不可少的。 前面我们简单总结了一些 Thrift 的一些基础知识点,但无非是一些细节层面的东西,所谓『细枝末节』也…

Spring Bean四种注入方式(Springboot环境)

阅读此文建议参考本人写的Spring常用注解:https://blog.csdn.net/21aspnet/article/details/104042826 给容器中注册组件的四种方法: 1.ComponentScan包扫描组件标注注解Component(ControllerServiceRepository) 使用场景:自己写的代码&…

chrome dev debug network 的timeline说明

在使用chrome的时候F12的开发者工具中有个network,其中对每个请求有个timeline的说明,当鼠标放上去会有下面的显示: 这里面的几个指标在说明在chrome使用文档有说明: 下面我用人类的语言理解下: Proxy 与代理服务器的连…

【MATLAB】函数句柄

在MATLAB平台中,对函数的调用方法分为直接调用法和间接调用法。 1、直接调用函数,被调用的函数通常称为子函数。一个文件中只能有一个主函数。 2、函数句柄——提供一种间接调用函数的方法。创建函数句柄需要用到操作符。 创建函数句柄的一般句法格式…

为什么企业选择年底裁员?如何选择一个正确的公司!

为什么很多企业选择年底裁员?首先分析一下裁员的原因:1、你能力不行,在公司吃闲饭2、减少公司成本3、公司换血,需要新的人才注入普通情况下,这些因素裁员很正常,只能怪自己不争气,成为末尾被淘汰…

springboot集成logback日志 通用logback.xml模板详解

先看Spring Boot中依赖的logback,log4j,slf4j相关Jar包 1.最简单的默认打印控制台日志 import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.Reques…

【MATLAB】单元数组类型

1、概述 单元(Cell)数组是一种无所不包的广义数组。 组成单元数组的每个元素成为一个单元。 每一个单元可以包括任意数组,如数值数组,字符串数组,结构体数组或另外一个单元数组。 单元数组用花括号来创建“{ }”。…

UNITY3D拓展编辑器 - 目录

前文:最近在自学UNITY3D拓展器,对以上功能点做一些认知范围内的总结.目录:1. 属性编辑器http://weizeteng.blog.51cto.com/5604545/17744312. 工具编辑器3. 场景编辑器转载于:https://blog.51cto.com/weizeteng/1774390

程序员的你还沉浸在大公司就是螺丝钉?小公司锻炼人?错了!看完即懂

刚毕业那会经历过很多所谓创业公司,和很多朋友经历过画大饼,洗脑以及公司上市原始股这样的承诺。当你正在趟过这些谎言你就会发现,在这个世界上能信这些鬼话的也只有涉世未深的毕业生了。小公司里真的就是十几二十几个精英带你一路向前&#…

深入Jetty源码之Servlet框架及实现(AsyncContext、RequestDispatcher、HttpSession)

概述 Servlet是Server Applet的缩写,即在服务器端运行的小程序,而Servlet框架则是对HTTP服务器(Servlet Container)和用户小程序中间层的标准化和抽象。这一层抽象隔离了HTTP服务器的实现细节,而Servlet规范定义了各个类的行为,从…

SpringBoot conditional注解和自定义conditional注解使用

conditional注解是Springboot starter的基石,自动装配的时候会根据条件确定是否需要注入这个类。 含义:基于条件的注解。 作用:根据是否满足某个特定条件来决定是否创建某个特定的Bean。 意义:Springboot实现自动配置的关键基础…

TOPSIS算法及代码

TOPSIS的全称是“逼近于理想值的排序方法” 根据多项指标、对多个方案进行比较选择的分析方法,这种方法的中心思想在于首先确定各项指标的正理想值和负理想值,所谓正理想值是一设想的最好值(方案),它的的各个属性值都…

django框架的基础知识点《贰》

状态保持-----session作用:状态保持与cookie区别: cookie保存在浏览器中 session:保存在服务器中,即python代码运行的那台电脑 支持配置,可以指定保存的位置在django中保存方案: 关系型数据库 内存 关系型数…

Springboot源码分析之内嵌tomcat源码分析

Springboot源码是内嵌tomcat的,这个和完整的tomcat还是不同。 内嵌tomcat的源码在tomcat-embed-core等3个jar包里 展开tomcat-embed-core的catalina目录 再对照下载的apache-tomcat-9.0.31源码 打开bin目录,看到很多库文件比如catalina.jar 再展开看看类…