python学习5

Posted yangjingxuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习5相关的知识,希望对你有一定的参考价值。

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

  1 # 1. use _ to connect entries in a list
  2 # if there are no numbers in list
  3 li = [alex,eric,rain]
  4 v = _.join(li)
  5 print(v)
  6 # if there are numbers in list
  7 li = [alex,eric,123]
  8 nli = []  # initialize a list new li (nli)
  9 for item in li:
 10     nli.append(str(item))  # add strred entries into list s
 11 v = "_".join(nli)
 12 print(nli,v)
 13 
 14 # 3. for a list
 15 li = [alex,eric,rain]
 16 # realize the following functions:
 17 # a. figure out the length of tuple and output the result
 18 print(len(li))
 19 # b. add "seven" into the list
 20 li.append(seven)
 21 print(li)
 22 # c. insert "Tony" at the first place
 23 li.insert(0,Tony)
 24 print(li)
 25 # d. insert "Kelly" at the second place
 26 li.insert(1,Kelly)
 27 print(li)
 28 # e. delete "eric"
 29 li.remove(eric)
 30 print(li)
 31 ## f. delete the second entry, output the entry deleted
 32 #v = li.pop(1)
 33 #print(v,li)
 34 ## g. delete the third entry, output the list
 35 #li.pop(2)
 36 #print(li)
 37 ## h. delete the entries from 2 to 4, output the list
 38 #del li[1:4]
 39 #print(li)
 40 # i. reverse the list
 41 li.reverse()
 42 print(li)
 43 # j. use for, len, range to output the index of the list
 44 for i in range(len(li)):
 45     print(i)
 46 # k. use enumerate to output entries and index from 100
 47 for idx, elem in enumerate(li,100):
 48     print(idx,elem)
 49 # l. use for to output all the entries
 50 for item in li:
 51     print(item)
 52 
 53 # 4. 
 54 
 55 # 5. for a tuple 
 56 tu = (alex,eric,rain)
 57 # realize the following functions:
 58 # a. figure out the length of tuple and output the result
 59 print(len(tu))
 60 # b. obtain the 2nd entry of the tuple and output it
 61 print(tu[1])
 62 # c. obtain the entries from 1 to 2 and output them
 63 print(tu[0:2])
 64 # d. use for to output all the entries of the tuple
 65 for item in tu:
 66     print(item)
 67 # e. use for, len, range to output the index of the tuple
 68 for i in range(0,len(tu)):
 69     print(i)
 70 # f. use enumrate output the entries and index of the tuple,
 71 # the index beginning at 10
 72 for idx, elem in enumerate(tu,10):
 73     print(idx,elem)
 74 
 75 # 6. for a tuple
 76 tu = ("alex",[11,22,{"k1":v1,"k2":["age","name"],"k3":(11,22,33)},44])
 77 # add an entry in the value corresponding to "k2" if addable
 78 tu[1][2]["k2"].append("Seven")
 79 print(tu)
 80 
 81 # 10. output goods list, costoms input the number and show the good w.r.t the index
 82 li = [iphone,laptop,cup,ship]
 83 idx = 1
 84 while idx > 0:
 85     good = input(please input the good added:)
 86     if bool(good):
 87         li.append(good) 
 88     idx = int(input(please input the number of the good:))
 89     print(li[idx-1])
 90 
 91 # 12. False in bool: 3 + 3 + 1
 92 # {}, [], ()
 93 # 0, "", False
 94 # None
 95 
 96 # 13. there are two lists:
 97 l1 = [11,22,33]
 98 l2 = [22,33,44]
 99 # a. obtain the same entries of the two lists
100 for i in l1:
101     if i in l2:
102         print(i)
103 # b. obtain the entries in l1 not in l2
104 for i in l1:
105     if i not in l2:
106         print(i)
107 # c. obtain the entries in l2 not in l1
108 for i in l2:
109     if i not in l1:
110         print(i)
111 # d. obtain the entries that different in l1 and l2
112 for i in l1:
113     if i not in l2:
114         print(i)
115 for i in l2:
116     if i not in l1:
117         print(i)
118 
119 # 16. display contents in different pages, one page with 10 items
120 user_list = []
121 for i in range(1,302):
122     temp = {name:alex+str(i),email:alex@alex.com+str(i)}
123     user_list.append(temp)
124 
125 page = 1
126 while page > 0 and page < 32:
127     page = int(input(please input the page you want to look:))
128     start = (page - 1)*10
129     end = page * 10
130     result = user_list[start:end]
131     for item in result:
132         print(item)
133 
134 # 17. the numbers of combanions of two numbers in 1-8, and the two digits are different
135 li = [1,2,3,4,5,6,7,8]
136 count = 0
137 l = len(li)
138 for i in range(l):
139     for j in range(l):
140         if i != j:
141             count += 1
142 print(count)
143 # 18. output 9*9 table
144 for i in range(1,10):
145     for j in range(i,10):
146         print(i,*,j,=,i*j,end = 	)
147     print(‘‘)
148 
149 # 19. for a list
150 nums = [2,7,11,15,1,8,7]
151 # find the pairs that the sum of which is 9
152 l = len(nums)
153 li = []
154 for i in range(l):
155     for j in range(i+1,l):
156         if nums[i] + nums[j] == 9:
157             li.append((nums[i],nums[j],))
158 print(li)
159 
160 # 20. a costs 5 yuan, b costs 3 yuan, 3c costs 1 yuan
161 #   use 100 yuan to buy 100 entities, give solutions
162 for x in range(1,100//5):
163     for y in range(1,100//3):
164         for z in range(1,100):
165             if x + y + z == 100 and 5*x + 3*y + z/3 == 100:
166                 print(x,y,z)
167 
168 # end of file                

 

以上是关于python学习5的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记:python3,代码片段(2017)

Python学习总结

python 学习python语法的片段

python小白学习记录 多线程爬取ts片段

python ipython:机器学习片段

30 段 Python 实用代码