python学习5—一些关于基本数据结构的练习题

 # 1. use _ to connect entries in a list
# if there are no numbers in list
li = ['alex','eric','rain']
v = '_'.join(li)
print(v)
# if there are numbers in list
li = ['alex','eric',123]
nli = [] # initialize a list new li (nli)
for item in li:
nli.append(str(item)) # add strred entries into list s
v = "_".join(nli)
print(nli,v) # 3. for a list
li = ['alex','eric','rain']
# realize the following functions:
# a. figure out the length of tuple and output the result
print(len(li))
# b. add "seven" into the list
li.append('seven')
print(li)
# c. insert "Tony" at the first place
li.insert(0,'Tony')
print(li)
# d. insert "Kelly" at the second place
li.insert(1,'Kelly')
print(li)
# e. delete "eric"
li.remove('eric')
print(li)
## f. delete the second entry, output the entry deleted
#v = li.pop(1)
#print(v,li)
## g. delete the third entry, output the list
#li.pop(2)
#print(li)
## h. delete the entries from 2 to 4, output the list
#del li[1:4]
#print(li)
# i. reverse the list
li.reverse()
print(li)
# j. use for, len, range to output the index of the list
for i in range(len(li)):
print(i)
# k. use enumerate to output entries and index from 100
for idx, elem in enumerate(li,100):
print(idx,elem)
# l. use for to output all the entries
for item in li:
print(item) # 4. # 5. for a tuple
tu = ('alex','eric','rain')
# realize the following functions:
# a. figure out the length of tuple and output the result
print(len(tu))
# b. obtain the 2nd entry of the tuple and output it
print(tu[1])
# c. obtain the entries from 1 to 2 and output them
print(tu[0:2])
# d. use for to output all the entries of the tuple
for item in tu:
print(item)
# e. use for, len, range to output the index of the tuple
for i in range(0,len(tu)):
print(i)
# f. use enumrate output the entries and index of the tuple,
# the index beginning at 10
for idx, elem in enumerate(tu,10):
print(idx,elem) # 6. for a tuple
tu = ("alex",[11,22,{"k1":'v1',"k2":["age","name"],"k3":(11,22,33)},44])
# add an entry in the value corresponding to "k2" if addable
tu[1][2]["k2"].append("Seven")
print(tu) # 10. output goods list, costoms input the number and show the good w.r.t the index
li = ['iphone','laptop','cup','ship']
idx = 1
while idx > 0:
good = input('please input the good added:')
if bool(good):
li.append(good)
idx = int(input('please input the number of the good:'))
print(li[idx-1]) # 12. False in bool: 3 + 3 + 1
# {}, [], ()
# 0, "", False
# None # 13. there are two lists:
l1 = [11,22,33]
l2 = [22,33,44]
# a. obtain the same entries of the two lists
for i in l1:
if i in l2:
print(i)
# b. obtain the entries in l1 not in l2
for i in l1:
if i not in l2:
print(i)
# c. obtain the entries in l2 not in l1
for i in l2:
if i not in l1:
print(i)
# d. obtain the entries that different in l1 and l2
for i in l1:
if i not in l2:
print(i)
for i in l2:
if i not in l1:
print(i) # 16. display contents in different pages, one page with 10 items
user_list = []
for i in range(1,302):
temp = {'name':'alex'+str(i),'email':'alex@alex.com'+str(i)}
user_list.append(temp) page = 1
while page > 0 and page < 32:
page = int(input('please input the page you want to look:'))
start = (page - 1)*10
end = page * 10
result = user_list[start:end]
for item in result:
print(item) # 17. the numbers of combanions of two numbers in 1-8, and the two digits are different
li = [1,2,3,4,5,6,7,8]
count = 0
l = len(li)
for i in range(l):
for j in range(l):
if i != j:
count += 1
print(count)
# 18. output 9*9 table
for i in range(1,10):
for j in range(i,10):
print(i,'*',j,'=',i*j,end = '\t')
print('') # 19. for a list
nums = [2,7,11,15,1,8,7]
# find the pairs that the sum of which is 9
l = len(nums)
li = []
for i in range(l):
for j in range(i+1,l):
if nums[i] + nums[j] == 9:
li.append((nums[i],nums[j],))
print(li) # 20. a costs 5 yuan, b costs 3 yuan, 3c costs 1 yuan
# use 100 yuan to buy 100 entities, give solutions
for x in range(1,100//5):
for y in range(1,100//3):
for z in range(1,100):
if x + y + z == 100 and 5*x + 3*y + z/3 == 100:
print(x,y,z) # end of file

python学习5—一些关于基本数据结构的练习题的更多相关文章

  1. (私人收藏)python学习(游戏、爬虫、排序、练习题、错误总结)

    python学习(游戏.爬虫.排序.练习题.错误总结) https://pan.baidu.com/s/1dPzSoZdULHElKvb57kuKSgl7bz python100经典练习题python ...

  2. Python学习系列----第六章 数据结构

    本章主要讲的是python中重要的四种数据结构,分别是列表.元组.字典和集合. 6.1 列表 list 是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目应该包括在方括 ...

  3. Python学习 Part3:数据结构

    Python学习 Part3:数据结构 1. 深入列表: 所有的列表对象方法 list.append(x): 在列表的末尾添加一个元素 list.extend(L): 在列表的末尾添加一个指定列表的所 ...

  4. Python学习(四)数据结构(概要)

    Python 数据结构 本章介绍 Python 主要的 built-type(内建数据类型),包括如下: Numeric types          int float Text Sequence ...

  5. Python学习-第二天-字符串和常用数据结构

    Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...

  6. python学习4—数据结构之列表、元组与字典

    python学习4—数据结构之列表.元组与字典 列表(list)深灰魔法 1. 连续索引 li = [1,1,[1,["asdsa",4]]] li[2][1][1][0] 2. ...

  7. python学习笔记五——数据结构

    4 . python的数据结构 数据结构是用来存储数据的逻辑结构,合理使用数据结构才能编写出优秀的代码.python提供的几种内置数据结构——元组.列表.字典和序列.内置数据结构是Python语言的精 ...

  8. python学习总结----简单数据结构

    mini-web服务器 - 能够完成简单的请求处理 - 使用http协议 - 目的:加深对网络编程的认识.为后面阶段学习web做铺垫 简单数据结构 - 排列组合 import itertools # ...

  9. 【Python五篇慢慢弹】数据结构看python

    数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...

随机推荐

  1. AutoCAD2016简体中文破解版32位64位下载

    AutoCAD2016序列号:666-69696969 667-98989898 400-45454545 066-66666666(任意一个) AutoCAD2016产品密钥:001H1 AutoC ...

  2. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码扩展)

    从别人那拷贝的 #region 定义和初始化配置字段 //用户存取验证码字符串 public string validationCode = String.Empty; //生成的验证码字符串 pub ...

  3. docker 详细安装及问题排查

    一.安装docker 1.Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname -r 命令查看 ...

  4. 函数中的toString

    function Person(){ this.name = name; this.age = age; this . gender = gender;  } //  创建一个Person实例 var ...

  5. pandas读书笔记 算数运算和数据对齐

    pandas最重要的一个功能是,它可以对不同索引的对象进行算数运算.在对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集. Series s1=Series([,3.4,1.5],ind ...

  6. echats问题

    echats 横轴显示不下datazoom配置,加入滚动条 实例博客  https://blog.csdn.net/Zheng_xiao_xin/article/details/80882113 常用 ...

  7. 项目到上传Gitee

    1.码云上创建一个项目 testgit (名字随你) 2.本地创建一个文件夹D:/testgit,然后使用git bash 3.cd 到本地文件夹中D:/testgit, 4.使用 git init ...

  8. Interview - 面试题汇总目录

    参考 java 入门面试题 https://blog.csdn.net/meism5/article/details/89021536 一.Java 基础 1.JDK 和 JRE 有什么区别? 2.= ...

  9. Batch - windows batch 常用命令(cheat sheet)

    原文地址:https://www.oschina.net/code/snippet_158297_4964 1 echo 和 @ 回显命令 @ #关闭单行回显 echo off #从下一行开始关闭回显 ...

  10. Kotlin -help

    { kotlin: run Kotlin programs, scripts or REPL. Usage: kotlin <options> <command> <ar ...