python学习5—一些关于基本数据结构的练习题
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—一些关于基本数据结构的练习题的更多相关文章
- (私人收藏)python学习(游戏、爬虫、排序、练习题、错误总结)
python学习(游戏.爬虫.排序.练习题.错误总结) https://pan.baidu.com/s/1dPzSoZdULHElKvb57kuKSgl7bz python100经典练习题python ...
- Python学习系列----第六章 数据结构
本章主要讲的是python中重要的四种数据结构,分别是列表.元组.字典和集合. 6.1 列表 list 是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目应该包括在方括 ...
- Python学习 Part3:数据结构
Python学习 Part3:数据结构 1. 深入列表: 所有的列表对象方法 list.append(x): 在列表的末尾添加一个元素 list.extend(L): 在列表的末尾添加一个指定列表的所 ...
- Python学习(四)数据结构(概要)
Python 数据结构 本章介绍 Python 主要的 built-type(内建数据类型),包括如下: Numeric types int float Text Sequence ...
- Python学习-第二天-字符串和常用数据结构
Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...
- python学习4—数据结构之列表、元组与字典
python学习4—数据结构之列表.元组与字典 列表(list)深灰魔法 1. 连续索引 li = [1,1,[1,["asdsa",4]]] li[2][1][1][0] 2. ...
- python学习笔记五——数据结构
4 . python的数据结构 数据结构是用来存储数据的逻辑结构,合理使用数据结构才能编写出优秀的代码.python提供的几种内置数据结构——元组.列表.字典和序列.内置数据结构是Python语言的精 ...
- python学习总结----简单数据结构
mini-web服务器 - 能够完成简单的请求处理 - 使用http协议 - 目的:加深对网络编程的认识.为后面阶段学习web做铺垫 简单数据结构 - 排列组合 import itertools # ...
- 【Python五篇慢慢弹】数据结构看python
数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...
随机推荐
- mysql恢复root密码
1.停止MySQL服务: // mysqld stopnet stop mysql 2.跳过权限检查启动MySQL(此时不要关闭CMD窗口): // mysqld -nt –skip-grant- ...
- [NOI2016]区间 题解(决策单调性+线段树优化)
4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1593 Solved: 869[Submit][Status][ ...
- 在Windows的控制台和Linux的终端中显示加载进度
Windows中 #include <stdio.h> #include <windows.h> int main() { ;//任务完成总量 int i; ; i < ...
- CSS template
ylbtech-CSS3: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.cn ...
- pefile解析PE格式
import os,sys import pefile import pydasm import struct #print sys.argv def show_section(pe): print ...
- C#基础-->cookie和session
关于cookie和session cookie 1:一个cookie中可以存放的数据最大在4KB左右 2:cookie存放于客户端 3:cookie分为两种 一种是会话cookie 一种是持久co ...
- Java目录事件
当文件系统中的对象被修改时,我们可以监听watch服务以获取警报.java.nio.file包中的以下类和接口提供watch服务. Watchable接口 WatchService接口 WatchKe ...
- springboot上传图片大小限制
背景:springboot项目上传图片超过1M报错,经了解,springboot默认上传文件1M 需求:更改默认配置,控制上传文件大小 方法:①更改配置文件(经试验不可行,不知道为什么):②更改启动B ...
- 微信小程序之评分页面
首先给大家看看做好的效果图: 一.接下来我们说一下评分这个功能: 实际上就是一个简单的js,首先我们遍历出小星星,此时默认给的五星好评,在给他们一个点击事件,当点击时,我们获取到当前点击的是第几颗:代 ...
- 从零开始搭建系统2.4——Jenkins安装及配置
1.安装wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.reporpm --impo ...