Python打卡Day21

Question 85

Question

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].


Hints

Use list comprehension to delete a bunch of element from a list.Use enumerate() to get (index, value) tuple.


Main author's Solution: Python 2

1li = [12,24,35,70,88,120,155]
2li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]
3print li

My Solution: Python 3

1li = [12,24,35,70,88,120,155]
2li = [li[i] for i in range(len(li)) if i not in (0,4,5)]
3print(li)

1'''Solution by: pratikb0501
2'''
3li = [12, 24, 35, 70, 88, 120, 155]
4print(list(j for i, j in enumerate(li) if i != 0 and i != 4 and i != 5))

Question 86

Question

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].


Hints

Use list's remove method to delete a value.


Main author's Solution: Python 2

1li = [12,24,35,24,88,120,155]
2li = [x for x in li if x!=24]
3print li

My Solution: Python 3

1li = [12,24,35,24,88,120,155]
2li.remove(24)  # this will remove only the first occurrence of 24
3print(li)

Question 87

Question

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.


Hints

Use set() and "&=" to do set intersection operation.


Main author's Solution: Python 2

1
2set1=set([1,3,6,78,35,55])
3set2=set([12,24,35,24,88,120,155])
4set1 &= set2
5li=list(set1)
6print li

My Solution: Python 3

1list1 = [1,3,6,78,35,55]
2list2 = [12,24,35,24,88,120,155]
3set1= set(list1)
4set2= set(list2)
5intersection = set1 & set2
6print(intersection)

OR

1list1 = [1,3,6,78,35,55]
2list2 = [12,24,35,24,88,120,155]
3set1= set(list1)
4set2= set(list2)
5intersection = set.intersection(set1,set2)
6print(intersection)

Question 88

Question

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.


Hints

Use set() to store a number of values without duplicate.


Main author's Solution: Python 2

 1def removeDuplicate( li ):
 2    newli=[]
 3    seen = set()
 4    for item in li:
 5        if item not in seen:
 6            seen.add( item )
 7            newli.append(item)
 8
 9    return newli
10
11li=[12,24,35,24,88,120,155,88,120,155]
12print removeDuplicate(li)

My Solution: Python 3

1li = [12,24,35,24,88,120,155,88,120,155]
2for i in li:
3    if li.count(i) > 1:
4        li.remove(i)
5print(li)

OR

 1def removeDuplicate( li ):
 2    seen = {}  # dictionary
 3    for item in li:
 4        if item not in seen:
 5            seen[item] = True
 6            yield item
 7
 8li = [12, 24, 35, 24, 88, 120, 155, 88, 120, 155]
 9ans = list(removeDuplicate(li))
10print(ans)

Question 89

Question

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.


Hints

Use Subclass(Parentclass) to define a child class.


Solution:

 1class Person(object):
 2    def getGender( self ):
 3        return "Unknown"
 4
 5class Male( Person ):
 6    def getGender( self ):
 7        return "Male"
 8
 9class Female( Person ):
10    def getGender( self ):
11        return "Female"
12
13aMale = Male()
14aFemale= Female()
15print aMale.getGender()
16print aFemale.getGender()

go to previous day

go to next day

Discussion