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 ...
随机推荐
- Jmeter断言-所有断言讲解
Jmeter断言-所有断言讲解 jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中 ...
- Intel Pin基础
参考:http://software.intel.com/sites/landingpage/pintool/docs/62732/Pin/html/ http://blog.nruns.com/bl ...
- 剑指offer——48把数字翻译成字符串
题目要求: 给定一个数字,按照如下规则翻译成字符串:0翻译成“a”,1翻译成“b”...25翻译成“z”.一个数字有多种翻译可能,例如12258一共有5种,分别是bccfi,bwfi,bczi,mcf ...
- shell脚本批量监控主机磁盘信息
写一个配置文件保存被监控主机SSH连接信息,格式:IP User Port [root@Test ~]# cat host 10.10.10.10 root 22 10.10.10.11 root 2 ...
- 深入理解JAVA虚拟机原理之Dalvik虚拟机(三)
更多Android高级架构进阶视频学习请点击:https://space.bilibili.com/474380680 本文是Android虚拟机系列文章的第三篇,专门介绍Andorid系统上曾经使用 ...
- 高并发下的缓存架构设计演进及redis常见的缓存应用异象解决方案
待总结 缓存穿透 缓存击穿 缓存雪崩等
- (数据科学学习手札60)用Python实现WGS84、火星坐标系、百度坐标系、web墨卡托四种坐标相互转换
一.简介 主流被使用的地理坐标系并不统一,常用的有WGS84.GCJ02(火星坐标系).BD09(百度坐标系)以及百度地图中保存矢量信息的web墨卡托,本文利用Python编写相关类以实现4种坐标系统 ...
- sanic+aiohttp爬虫demo(爬图片,新闻,数据)
直接上代码,都是很简单的一些demo,爬取的网站,都没有什么加密措施,所以应该不涉及违法数据,哈哈 1.爬取网页数据(aiohttp+sanic+scrapy+xpath解析html) from sa ...
- 【转】谈一谈 Normalize.css
原文链接: https://www.jianshu.com/p/9d7ff89757fd 笔记: 如何使用?
- Jmeter-Json提取器、用户定义变量配置
一.Jmeter用户定义的变量,一般用于配置全局变量 1.选择用户定义的变量菜单 2.配置需要的用户定义变量 这里我添加常用的localhost和port 3.如何使用 需要使用${...}进行引用 ...