昨天和你們分享了71-80題,今天繼續來刷81~90題git
By using list comprehension, please write a program to print the list after removing numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [x for x in li if x % 35!=0] print(li)
By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [li[i] for i in range(len(li)) if i%2 != 0] print(li)
li = [12,24,35,70,88,120,155] li = [x for (i,x) in enumerate(li) if i%2!=0] print(li)
By using list comprehension, please write a program to print the list after removing the 2nd - 4th numbers in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [x for (i,x) in enumerate(li) if i<3 or 4<i] print(li)
By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0.
array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)] print(array)
By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155].
li = [12,24,35,70,88,120,155] li = [li[i] for i in range(len(li)) if i not in (0,4,5)] print(li)
By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155].
li = [12,24,35,24,88,120,155] li.remove(24) # this will remove only the first occurrence of 24 print(li)
li = [12,24,35,24,88,120,155] li = [x for x in li if x!=24] # Delete all 24 print(li)
With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists.
set1=set([1,3,6,78,35,55]) set2=set([12,24,35,24,88,120,155]) result = set1.intersection(set2) print(result)
set1=set([1,3,6,78,35,55]) set2=set([12,24,35,24,88,120,155]) result = set1 & set2 print(result)
With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved.
li=[12,24,35,24,88,120,155,88,120,155] result = set(li) #> Python 3.6 Set keep the order test print(result)
def removeDuplicate( li ): seen = {} for item in li: if item not in seen: seen[item] = True yield item li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155] ans = list(removeDuplicate(li)) print(ans)
li = [12,24,35,24,88,120,155,88,120,155] for i in li: if li.count(i) > 1: li.remove(i) print(li)
Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class.
class Person(object): def getGender( self ): return "Unknown" class Male( Person ): def getGender( self ): return "Male" class Female( Person ): def getGender( self ): return "Female" # Test aMale = Male() aFemale= Female() print(aMale.getGender()) print (aFemale.getGender())
Please write a program which count and print the numbers of each character in a string input by console.*Example:
If the following string is given as input to the program:*github
abcdefgabc
Then, the output of the program should be:
a,2 c,2 b,2 e,1 d,1 g,1 f,1
s=input() dic = {word:s.count(word) for word in s} for k, v in dic.items(): print(f"{k},{v}")
s = input() for letter in range(ord('a'),ord('z')+1): # ord() gets the ascii value of a char letter = chr(letter) # chr() gets the char of an ascii value cnt = s.count(letter) if cnt > 0: print("{},{}".format(letter,cnt))
這十道題的代碼在個人github上,若是你們想看一下每道題的輸出結果,能夠點擊如下連接下載:this
個人運行環境Python 3.6+,若是你用的是Python 2.7版本,絕大多數不一樣就體如今如下3點:code
謝謝你們,咱們下期見!但願各位朋友不要吝嗇,把每道題的更高效的解法寫在評論裏,咱們一塊兒進步!!!orm