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

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':'[email protected]'+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
05-11 16:22