python数据结构题目_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 inli: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 inrange(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 inli: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 intu:66 print(item)67 #e. use for, len, range to output the index of the tuple

68 for i inrange(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 ifbool(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 inl1:101 if i inl2:102 print(i)103 #b. obtain the entries in l1 not in l2

104 for i inl1:105 if i not inl2:106 print(i)107 #c. obtain the entries in l2 not in l1

108 for i inl2:109 if i not inl1:110 print(i)111 #d. obtain the entries that different in l1 and l2

112 for i inl1:113 if i not inl2:114 print(i)115 for i inl2:116 if i not inl1: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 inresult: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 =0137 l =len(li)138 for i inrange(l):139 for j inrange(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 = '\t')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 inrange(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值