Write a function secondLst() that takes a two-dimensional list as a parameter an
ID: 3741400 • Letter: W
Question
Write a function secondLst() that takes a two-dimensional list as a parameter and returns a new list containing all of the integers found in the list. The list can contain any type of item. The type() function can determine what type of object is currently stored in a variable and is helpful for this question. If the list is empty or the list contains no integers, the function should return the empty list. The function will ignore integers found in other types (e.g. dictionaries, tuples, etc.). The function should not change the list passed as a parameter in any way.
This is the output in python please.
Python 3.6.4 Shell File Edit She Debug Options Window Help >>> lst1 - [' test', 1], ['to, 2, 3.5], [(1.2, 5), 3.14]] >>> 11 - buildIntList(lstl) >>>1st1 'test', 1], ['two', 2, 3.5], [(1.2, 5), 3.14]] >>> 12 = buildint List ( [?one' ] , ?two. 2 . 2j, ?three' , 3, 3 . 5j, >>>12 ?four- 4, 4 . 44, test' ] , ?five' ] ] ) buildint List ( [?one' ] , [ ? two' ] , [Truej, ?three' ] , [ { 1 :2 } ] , ?four' ] ] ) >>> 13 >>>13 >>> 14 - buildIntList ([]) >>> 14 Ln: 25 Col: 4Explanation / Answer
def buildList(lst1):
arr = []
# read one row at a time
for x in lst1:
# read one element of current row at a time
for y in x:
# if y is of type int
if type(y) == type(1):
#print('hi')
arr.append(y)
return arr
lst1 = [ ['test' , 1], [ 'two', 2, 3.5 ], [(1.2, 5) , 3.14] ]
print(buildList(lst1))
lst2 = [ ['one'], ['two'], [True], ['three'], [{1:2}], ['four'] ]
print(buildList(lst2))
Sample Output
[ 1 , 2 ]
[ ]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.