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...
write a Python program (with comments) to do the following: Define a list list1 = [['Rose',...
write a Python program (with comments) to do the following: Define a list list1 = [['Rose', 'Daisy'], 7, 0, 5.5, [1, 22, 2.45, 3, 6, 5.8]] and print it. Print the length of list1 (i.e. number of items in list1) with a suitable message. Print the first character of the first string in the first item of list1 with a suitable message (HINT: The first item is the item with index 0). Print the sum of the last 4 numbers...
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...
Group A Group B 1 5 5 8 5 6 5 7 5 8 6 8...
Group A Group B 1 5 5 8 5 6 5 7 5 8 6 8 7 9 6 16 10 20 13 24 At the significance level of 0.05, are the drug concentrations in Group A and Group B different? Using the most appropriate t test.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT