元组(tuple)

 #元组相对列表来说不同之处是只读不可写 读操作和列表一致
 letter_tuple = ('a','b','c','d');
 print(letter_tuple[0]);#result:a
 print(letter_tuple[0:3]);#result:['a', 'b', 'c']
 print(letter_tuple[3::-1]);#result:['c', 'b', 'a']

字典 (dictionary)

 addressDic = {'湖北':['武汉','襄阳'],'湖南':['长沙','岳阳']}
 for province in addressDic:
     cityList = addressDic[province];
     print(province,end = ":");
     for city in cityList:
         print(city,end = ";");
     print();
 #result:
     # 湖北: 武汉;襄阳;
     # 湖南: 长沙;岳阳;

字符串 (string)

 letterStr = 'abcdefg';
 #重复输出
 print(letterStr*2);#result:abcdefgabcdefg
 #切片
 print(letterStr[2:]);#result:cdefg
 #判断是否存在
 print('abc' in letterStr);#result:True
 #格式化输出
 print('abcd%s'%'efg');

 #string的内置方法
 #获取元素个数
 print(letterStr.count('c'));#result:1
 #字符串首字母大写
 print(letterStr.capitalize());#result:Abcdefg
 #指定长度居中
 print(letterStr.center(20,'-'));#result:------abcdefg-------
 #是否以指定元素结尾
 print(letterStr.endswith("g"));#result:True
 #是否以指定元素开头
 print(letterStr.startswith("a"));#result:True
 #指定tab空格长度
 print("a\tb".expandtabs(tabsize=10));#result:a         b
 #返回指定第一个元素索引值
 print(letterStr.find('c'));#result:2
 #格式化
 print("hello {next}".format(next="world"));#result:hello world
 print("name:{name};{age} years old".format_map({"name":"zhangsan","age":12}));#result:name:zhangsan;12 years old
 #返回指定元素索引 不存在时会报错
 print(letterStr.find('c'));#result:2
 #判断只包含数字或字母
 print("deqad#".isalnum());#result:False;
 #判断是否是整形数字
 '.isdigit());#result:True
 #判断是否不是非法命名
 print("123a".isidentifier());#result:False
 #判断是否小写
 print(letterStr.islower());#result:True
 #判断是否大写
 print(letterStr.isupper());#result:False
 #判断是否是个空格
 print(letterStr.isspace());#result:False
 #判断是否每个单词首字母大写
 print(letterStr.istitle())#result:False
 #转小写
 print(letterStr.lower());#result:abcdefg
 #转大写
 print(letterStr.upper());#result:ABCDEFG
 #大小写反转
 print(letterStr.swapcase());#result:ABCDEFG
 #左填充
 print(letterStr.ljust(20,'*'));#abcdefg*************
 #右填充
 print(letterStr.rjust(20,'*'));#*************abcdefg
 #去首尾空白字符
 print("  ade  \n".strip());#result:ade
 #去首部空白字符
 print("  ade  ".lstrip());#result:ade
 #去尾部空白字符
 print("  ade  \n".rstrip());#result:  ade
 #替换
 print(letterStr.replace('g','ghi'));#result:abcdefghi
 #从右往左查找 返回实际位置
 print("acbbbbcss".rfind('c'));#result:6
 #切割字符串 返回列表
 print(letterStr.split('d'));#result:['abc', 'efg']
 #字符串内每个单词首字母大写
 print('hello world'.title());#result:Hello World

队列(queen)

 import queue

 q = queue.Queue()
 q.put(1)
 q.put(2)
 q.put(3)
 print(q.qsize())  # 3 查看队列元素个数

 print(q.get())  # 阻塞

练习

三级联动

 mainDic = {
     'a1':{
         'a1b1':{
             'a1b1c1':{},
             'a1b1c2':{},
             'a1b1c3':{}
         },
         'a1b2':{
             'a1b2c1': {},
             'a1b2c2': {},
             'a1b2c3': {}
         },
         'a1b3':{
             'a1b3c1': {},
             'a1b3c2': {},
             'a1b3c3': {}
         }
     },
     'a2':{
         'a2b1': {
             'a2b1c1': {},
             'a2b1c2': {},
             'a2b1c3': {}
         },
         'a2b2': {
             'a2b2c1': {},
             'a2b2c2': {},
             'a2b2c3': {}
         },
         'a2b3': {
             'a2b3c1': {},
             'a2b3c2': {},
             'a2b3c3': {}
         }
     },
     'a3':{
         'a3b1': {
             'a3b1c1': {},
             'a3b1c2': {},
             'a3b1c3': {}
         },
         'a3b2': {
             'a3b2c1': {},
             'a3b2c2': {},
             'a3b2c3': {}
         },
         'a3b3': {
             'a3b3c1': {},
             'a3b3c2': {},
             'a3b3c3': {}
         }
     }
 };
 currentLayer = mainDic;
 parentLayers = [];
 while True:
     for item in currentLayer:
         print(item);
     choose = input('input your choose:');
     if(choose in currentLayer):
         if not currentLayer[choose]:
             print('last layer');
             continue;
         parentLayers.append(currentLayer);
         currentLayer = currentLayer[choose];
     elif choose=='e':
         currentLayer = parentLayers.pop();
     elif choose=='exit':
         break;
     else:
         print('该项不存在');

 #result:
     # >>a1
     # >>a2
     # >>a3
     # >>input your choose:a2
     # >>a2b1
     # >>a2b2
     # >>a2b3
     # >>input your choose:a2b2
     # >>a2b2c1
     # >>a2b2c2
     # >>a2b2c3
     # >>input your choose:e
     # >>a2b1
     # >>a2b2
     # >>a2b3
     # >>input your choose:e
     # >>a1
     # >>a2
     # >>a3
     # >>input your choose:exit

扩展

批量赋值

 a,b = [1,2];
 print(a);#result:1
 print(b);#result:2

enumerate(iterable:遍历对象,start:起始下标)

该函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

letter_tuple = ['a','b','c','d','e'];
for index,value in enumerate(letter_tuple,1):
    print(index,value);
#result:
    # (1, 1)
    # (2, 2)
    # (3, 3)
    # (4, 4)
    # (5, 5)

取序列长度

 letterStr = 'abcdefg';
 letterList = ['a','b','c','d','e','f','g'];
 print(len(letterStr));#result:7
 print(len(letterList));#result:7

可把序列当做判断条件,为空时返回False,反之True

 list = [];
 if list:print('True');
 else:print('False');
 #result:False;
 list.append(1);
 if list:print('True');
 else:print('False')
 #result:True

python基础(4)-元组&字典&字符串&队列的更多相关文章

  1. python3速查参考- python基础 4 -> 元组 + 字典 + 字符串 的学习

    元组 元组:特点就是内容不可变,算只读的列表,可以被查询,不能被修改 a = 2, print(a) print(type(a)) b = ('a','b','c') print(b[1]) 运行结果 ...

  2. python基础深入(元组、字符串、列表、字典)

    python基础深入(元组.字符串.列表.字典) 一.列表 1.追加 >>>list = [1,2,3,4] #用于在列表末尾添加新的对象,只能单个添加,该方法无返回值,但是会修改原 ...

  3. 2.python基础深入(元组、字符串、列表、字典)

    一,对象与类 对象: python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,玩的手机就是对象. 我们通过描述属性(特征)和行为来描述一个对象的. 在python中,一个对象的特 ...

  4. python基础-列表元组字典

    1.列表和元组 列表可以对数据实现最方便的存储.修改等操作 names=["Alex","tenglan","Eric","Rai ...

  5. Python学习---列表/元组/字典/字符串/set集合/深浅拷贝1207【all】

    1.列表 2.元组 3.字典 4.字符串 5.set集合 6.深浅拷贝

  6. python之列表/元组/字典/字符串

    一.列表 格式:list = ['xxx','xxx','xxx'] 性质:可以修改列表内容 copy用法: import copy names = ['] names01 = names #直接引用 ...

  7. 『Python基础-10』字典

    # 『Python基础-10』字典 目录: 1.字典基本概念 2.字典键(key)的特性 3.字典的创建 4-7.字典的增删改查 8.遍历字典 1. 字典的基本概念 字典一种key - value 的 ...

  8. Python基础知识(五)------字典

    Python基础知识(四)------字典 字典 一丶什么是字典 ​ dict关键字 , 以 {} 表示, 以key:value形式保存数据 ,每个逗号分隔 ​ 键: 必须是可哈希,(不可变的数据类型 ...

  9. python基础数据类型--元组(tuple)

    python基础数据类型--元组(tuple) 一.元组的定义和特性 定义:与列表相似,只不过就是将[ ] 改成 ( ) 特性:1.可以存放多个值 2.不可变 3.按照从左到右的顺序定义元组元素,下标 ...

随机推荐

  1. atitit r9 doc on home ntpc .docx

    卷 p2soft 的文件夹 PATH 列表 卷序列号为 9AD0-D3C8 D:. │  Aittit pato 面对拒绝  的回应.docx │  Atitit  中国明星数量统计 attilax. ...

  2. CNN(卷积神经网络)、RNN(循环神经网络)、DNN,LSTM

    http://cs231n.github.io/neural-networks-1 https://arxiv.org/pdf/1603.07285.pdf https://adeshpande3.g ...

  3. Build GMP on 64bit Windows

    1.MSYS2 环境搭建 1.1.安装 msys2 的主页地址: http://www.msys2.org/ 下载32位或64位,我这里 下载了64位 msys2-x86_64-20161025.ex ...

  4. zookeeper入门及使用(二)- 状态查看

    查看服务的角色,看Mode字段,有follower及leader [root@c7bit1 bin]# echo stat | nc 127.0.0.1 2181 Zookeeper version: ...

  5. textarea 分割

    var orderNo = $("#orderNo").val();var orderNo = orderNo.toString().split(/\r?\n/);

  6. Groovy和Java互调

    Scala和Java为静态语言,Groovy为动态语言 Scala: 函数式编程,同时支持面向对象 Groovy: jvm上的脚本,较好兼容java语法,Groovy加强了Java集成. 可配置化的优 ...

  7. Thinkphp5.1 模板路径报错

    版本:5.1.24   ,windows环境 报错: 模板文件不存在:template\index\default\index\index.html 1.报错原因:linux/windows   对大 ...

  8. windows 10 更新补丁包

    http://www.catalog.update.microsoft.com/Search.aspx?q=windows%2010%20prohttp://www.catalog.update.mi ...

  9. Redis密码设置与访问限制

    https://www.cnblogs.com/ghjbk/p/7682041.html https://ruby-china.org/topics/28094

  10. Bash script set help function

    set -o nounset help() { cat <<- EOF Desc: execute f1x for each case in Codeflaws Usage: ./exec ...