Question

In: Computer Science

This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2]...

This program is about list comprehension.

list1 = [2, 5, 7, 8]

list2 = [1, 2]

(e) Use nested list comprehension with list1 and list2 as input sequences to generate this list:

[[3, 4], [6, 7], [8, 9], [9, 10]]

Display the list.

The following is the expected output.

Part e: [[3, 4], [6, 7], [8, 9], [9, 10]]

Solutions

Expert Solution

Code:

list1 = [2, 5, 7, 8]
list2 = [1, 2]
l=[]
for i in range(0,len(list1)):
   a=[]
   for j in range(0,len(list2)):
       a.append(list1[i]+list2[j])
   l.append(a)

print(l)
Code in image with explanation:

Output:


Related Solutions

By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write...
By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write a python program which does the following: a. add items in list2 to list1 b. Remove mary from list1 c. Insert sam at 5th position d. Sort the elements of list1 in ascending order 2. Write a python program that asks the user to enter a sentence and creates a list of words in that sentence. 3. friends = ('sam','andy','mary','andy','john','andy','mary') Write a python program...
Consider the matrix list x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Write...
Consider the matrix list x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Write a list comprehension to extract the first column of the matrix [1, 4, 7]. Write another list comprehension to create a vector of twice the square of the middle column.
1.Consider the program: .data myArray: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...
1.Consider the program: .data myArray: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 .text la $s0, myArray li $s1, 0 loop: sll $t0, $s1, 2 add $t0, $t0, $s0 lw $s2, 0($t0) lw $s3, 4($t0) add $s2, $s2, $s3 sw $s2, 0($t0) addi $s1, $s1, 1 slti $t1, $s1, 9 bne $t1, $zero, loop .end Explain what does this program do? How is the data bound from the .data segment to the base address register $s0? What...
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5]...
A = [4, 5, 9] B = [-4, 5, -7] C = [2, -7, -8, 5] D = [1, -9, 5, -3] E = [3, 3, -1] Uz = 1/|z| ^z d(X,Y) = (Rθ) d = diameter R = Radius θ = Theta Find a. Uc b. d (D, C) c. Let P = B + 3E, UP = d. A x B e. 3B x E f. C x D
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisons made. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort.
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisonsmade. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
From this Sample No A.   B.      1. 7.   4.      2. 8.   5.      3....
From this Sample No A.   B.      1. 7.   4.      2. 8.   5.      3. 9.   3.      4. 5.   6.      5. 7.   2.      6. 6.   4.   Test of hypothesis for difference of two means by using 1. t Test 2. ANOVA Take 5% type 1 error, then compare the results. Show that t² is equal to f.
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7...
x 2 8 5 9 4 3 9 6 7 8 y 3 6 5 7 9 7 4 6 9 9 -5.48x + 0.17 5.48x + 0.17 -0.17x + 5.48 0.17x + 5.48
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT