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 ...
随机推荐
- windows server2012r2 安装NET Framework 3.5
在Windows Server 2012上安装一些软件,比如Oracle 11g等,经常会出现下面这样的错误:“无法安装一下功能:.NET Framework 3.5(包括.NET 2.0和3.0)” ...
- legend2---开发日志16
legend2---开发日志16 一.总结 一句话总结: 编程敲代码,一定要把 关系弄清楚,关系不弄清楚,很容易敲错,比如:如何求无限级分类的博客的数据的数目 弄清楚关系式:自己总数量=孩子总数量+自 ...
- js分割url提取参数
//分割url提取参数 var url = Window.location.search;//获取url地址?至结尾的所有参数 //key(需要检错的键) url(传入的需要分割的url地址) fun ...
- python 参数定义库argparse
python 参数定义库argparse 这一块的官方文档在这里 注意到这个库是因为argparse在IDE中和在ipython notebook中使用是有差异的,习惯了再IDE里面用,转到ipyth ...
- Vue 学习笔记之 —— 组件(踩了个坑)
最近在学习vue,学习组件时,遇到了一个问题,困扰了半个多小时.. <!DOCTYPE html> <html lang="en"> <head> ...
- 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷
通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEclipse中的window->show View->ot ...
- python语言和R语言实现机器学习算法
<转>机器学习系列(9)_机器学习算法一览(附Python和R代码) 转自http://blog.csdn.net/han_xiaoyang/article/details/51191 ...
- Google 公司的代码规范
如题: C++ , Objective-C, Java, Python, R, Shell, HTML/CSS, JavaScript, AngularJS, Common Lisp ,Vimscri ...
- wkhtmltopdf linux下html转pdf
https://blog.csdn.net/wujunlei1595848/article/details/91129197 https://github.com/wkhtmltopdf/wkhtml ...
- 通过网络socket获取对方 ip 和port
int getpeername(int s, struct sockaddr *name, socklen_t *namelen);描述获取socket的对方地址struct sockaddr_in ...