成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

Python基礎(chǔ)練習(xí)100題 ( 81~ 90)

劉德剛 / 1930人閱讀

摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn)

刷題繼續(xù)

昨天和大家分享了71-80題,今天繼續(xù)來(lái)刷81~90題

Question 81:
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)
Question 82:
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)
Question 83:
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
Question 84:
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)
Question 85:
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)
Question 86:
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)
Question 87:
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)
Question 88:
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)
Question 89:
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())
Question 90:
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:*

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上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載:

Python 81-90題

我的運(yùn)行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點(diǎn):

raw_input()在Python3中是input()

print需要加括號(hào)

fstring可以換成.format(),或者%s,%d

謝謝大家,我們下期見(jiàn)!希望各位朋友不要吝嗇,把每道題的更高效的解法寫(xiě)在評(píng)論里,我們一起進(jìn)步!?。?/p>

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/44000.html

相關(guān)文章

  • Python基礎(chǔ)練習(xí)100 ( 91~ 100

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷最后的題解法一解法二解法一解法二解法一雞兔同籠解法一解法一解法二解法一解法二默認(rèn)就是 刷題繼續(xù) 昨天和大家分享了81-90題,今天繼續(xù)來(lái)刷最后的91-100題 Question 91: Please write a program which accepts a string from console and print it in rever...

    Jrain 評(píng)論0 收藏0
  • 測(cè)試開(kāi)發(fā)必看:《笨辦法學(xué)Python3》PDF中文高清版,豆瓣高分8.0

    摘要:笨辦法學(xué)第版結(jié)構(gòu)非常簡(jiǎn)單,共包括個(gè)習(xí)題,其中個(gè)覆蓋了輸入輸出變量和函數(shù)三個(gè)主題,另外個(gè)覆蓋了一些比較高級(jí)的話題,如條件判斷循環(huán)類(lèi)和對(duì)象代碼測(cè)試及項(xiàng)目的實(shí)現(xiàn)等。最后只想說(shuō),學(xué)習(xí)不會(huì)辜負(fù)任何人,笨辦法學(xué) 內(nèi)容簡(jiǎn)介   《笨辦法學(xué)Python(第3版)》是一本Python入門(mén)書(shū)籍,適合對(duì)計(jì)...

    不知名網(wǎng)友 評(píng)論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 71~ 80)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下 刷題繼續(xù) 昨天和大家分享了61-70題,今天繼續(xù)來(lái)刷71~80題 Question 71: Please write a program to out...

    Jeff 評(píng)論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 31~ 40)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn)行環(huán)境如果你 刷題繼續(xù) 昨天和大家分享了21-30題,今天繼續(xù)來(lái)刷31~40題 Question 31: Define a function which can pr...

    miracledan 評(píng)論0 收藏0
  • Python基礎(chǔ)練習(xí)100 ( 51~ 60)

    摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載 刷題繼續(xù) 昨天和大家分享了41-50題,今天繼續(xù)來(lái)刷51~60題 Question 51: Write a function to compute 5/0 and use ...

    岳光 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<