How to pair two list in python - python-3.x

I think someone may have asked this question already, but for some reasons I just cannot come out good key words to find the answers for it.
I have two separate lists, and I could like to pair them.
list_a = [[1,2] [3,4]]
list_b = [[5],[6]]
I would like to generate:
list_c = [[[1,2],[5]],[[3,4],[6]]]
Thank you for your help

The following code should do the trick!
list_c = [[x, y] for x, y in zip(list_a, list_b)]
The zip function acts to 'pair' the list elements together, while the list comprehension builds the new list.

If you want to append them to a new list, this is what you want:
list_a = [[1,2], [3,4]]
list_b = [[5],[6]]
list_res = []
for a, b in zip(list_a, list_b):
list_res.append([a, b])
>list_res
>[[[1, 2], [5]], [[3, 4], [6]]]

Related

Modulo 10^9 +7 python

I have a list let's say : [1,3,5,6....,n] to very large number, that I need to report each number in the list in a new list but in "modulo 10^9+7" format in python.
How do I do it please?
I tried to search for it and people answer it's (n%m=p) and the solution is p, but I guess that's not it.
Some ways to do it. The first method is called list comprehension, and you can find many examples of it on stack overflow and elsewhere.
list1 = [1, 3, 5, 6, 201, 99121, 191929912, 8129391828989123]
modulus = 10**9 + 7
list2 = [x % modulus for x in list1] # example of list comprehension
or using map
list2 = list(map(lambda x: x % modulus, list1))
or perhaps the least elegant
list2 = []
for x in list1:
list2.append(x % modulus)

How to multiply 2 input lists in python

Please help me understand how to code the following task in Python using input
Programming challenge description:
Write a short Python program that takes two arrays a and b of length n
storing int values, and returns the dot product of a and b. That is, it returns
an array c of length n such that c[i] = a[i] · b[i], for i = 0,...,n−1.
Test Input:
List1's input ==> 1 2 3
List2's input ==> 2 3 4
Expected Output: 2 6 12
Note that the dot product is defined in mathematics to be the sum of the elements of the vector c you want to build.
That said, here is a possibiliy using zip:
c = [x * y for x, y in zip(a, b)]
And the mathematical dot product would be:
sum(x * y for x, y in zip(a, b))
If the lists are read from the keyboard, they will be read as string, you have to convert them before applying the code above.
For instance:
a = [int(s) for s in input().split(",")]
b = [int(s) for s in input().split(",")]
c = [x * y for x, y in zip(a, b)]
Using for loops and appending
list_c = []
for a, b in zip(list_a, list_b):
list_c.append(a*b)
And now the same, but in the more compact list comprehension syntax
list_c = [a*b for a, b in zip(list_a, list_b)]
From iPython
>>> list_a = [1, 2, 3]
>>> list_b = [2, 3, 4]
>>> list_c = [a*b for a, b in zip(list_a, list_b)]
>>> list_c
[2, 6, 12]
The zip function packs the lists together, element-by-element:
>>> list(zip(list_a, list_b))
[(1, 2), (2, 3), (3, 4)]
And we use tuple unpacking to access the elements of each tuple.
From fetching the input and using map & lambda functions to provide the result. If you may want to print the result with spaces between (not as list), use the last line
list1, list2 = [], []
list1 = list(map(int, input().rstrip().split()))
list2 = list(map(int, input().rstrip().split()))
result_list = list(map(lambda x,y : x*y, list1, list2))
print(*result_list)
I came out with two solutions. Both or them are the ones that are expected in a Python introductory course:
#OPTION 1: We use the concatenation operator between lists.
def dot_product_noappend(list_a, list_b):
list_c = []
for i in range(len(list_a)):
list_c = list_c + [list_a[i]*list_b[i]]
return list_c
print(dot_product_noappend([1,2,3],[4,5,6])) #FUNCTION CALL TO SEE RESULT ON SCREEN
#OPTION 2: we use the append method
def dot_product_append(list_a, list_b):
list_c = []
for i in range(len(list_a)):
list_c.append(list_a[i]*list_b[i])
return list_c
print(dot_product_append([1,2,3],[4,5,6])) #FUNCTION CALL TO SEE RESULT ON SCREEN
Just note that the first method requires that you cast the product of integers to be a list before you can concatenate it to list_c. You do that by using braces ([[list_a[i]*list_b[i]] instead of list_a[i]*list_b[i]). Also note that braces are not necessary in the last method, because the append method does not require to pass a list as parameter.
I have added the two function calls with the values you provided, for you to see that it returns the correct result. Choose whatever function you like the most.

How can I build new list from the old one in Python adding two new elements at a time?

I work with a tree and my code looks like this
new_lst = [x.left for x in lst if x.left] + [x.right for x in lst if x.right]
Is there an elegant way to rewrite it with just one list comprehension?
Thank you!
You should iterate over the tuple (x.left, x.right) inside the list comprehension.
>>> from collections import namedtuple
>>> Tree = namedtuple('Tree', ['left', 'right'])
>>> L = [Tree(1,2), Tree(0,3), Tree(4,0)]
(for the example, left and right are numbers; they should obviously be subtrees or None).
Your version:
>>> [x.left for x in L if x.left] + [x.right for x in L if x.right]
[1, 4, 2, 3]
In one pass:
>>> [t for x in L for t in (x.left, x.right) if t]
[1, 2, 3, 4]
Note that the order is different.

Convert list of lists of numbers into a list of lists with the numbers separated by commas

I have a list of lists like the following one:
list = [[226], [44], [8]]
I'd like to convert this list of lists into a list of lists where the numbers are separated by commas. Something like this:
[[2,2,6], [4,4], [8]]
I have been trying to do this using the split function. I didn't find a duplicate for this question.
This will work:
foo = [[226], [44], [8]]
foo = [eval('[' + ','.join(str(i[0])) + ']') for i in foo]
Or this will work:
foo = [[226], [44], [8]]
foo = [[int(n) for n in str(i[0])] for i in foo]
You can create something simple like this:
foo = [ 'a', 'b', 'c' ]
print ",".join(foo)
a,b,c
Hope this helps
You can do something like this. This will work even if you have multiple elements in each sublist like
[[226,542], [44], [8]]
def list_of_list(list):
result = []
for l in list:
for item in l:
sub_list= []
for c in str(item):
sub_list.append(int(c))
result.append(sub_list)
return result
print(list_of_list([[226], [44], [8]]))
Output:
[[2, 2, 6], [4, 4], [8]]

Python: How to find the average on each array in the list?

Lets say I have a list with three arrays as following:
[(1,2,0),(2,9,6),(2,3,6)]
Is it possible I get the average by diving each "slot" of the arrays in the list.
For example:
(1+2+2)/3, (2+0+9)/3, (0+6+6)/3
and make it become new arraylist with only 3 integers.
You can use zip to associate all of the elements in each of the interior tuples by index
tups = [(1,2,0),(2,9,6),(2,3,6)]
print([sum(x)/len(x) for x in zip(*tups)])
# [1.6666666666666667, 4.666666666666667, 4.0]
You can also do something like sum(x)//len(x) or round(sum(x)/len(x)) inside the list comprehension to get an integer.
Here are couple of ways you can do it.
data = [(1,2,0),(2,9,6),(2,3,6)]
avg_array = []
for tu in data:
avg_array.append(sum(tu)/len(tu))
print(avg_array)
using list comprehension
data = [(1,2,0),(2,9,6),(2,3,6)]
comp = [ sum(i)/len(i) for i in data]
print(comp)
Can be achieved by doing something like this.
Create an empty array. Loop through your current array and use the sum and len functions to calculate averages. Then append the average to your new array.
array = [(1,2,0),(2,9,6),(2,3,6)]
arraynew = []
for i in range(0,len(array)):
arraynew.append(sum(array[i]) / len(array[i]))
print arraynew
As you were told in the comments with sum and len it's pretty easy.
But in python I would do something like this, assuming you want to maintain decimal precision:
list = [(1, 2, 0), (2, 9, 6), (2, 3, 6)]
res = map(lambda l: round(float(sum(l)) / len(l), 2), list)
Output:
[1.0, 5.67, 3.67]
But as you said you wanted 3 ints in your question, would be like this:
res = map(lambda l: sum(l) / len(l), list)
Output:
[1, 5, 3]
Edit:
To sum the same index of each tuple, the most elegant method is the solution provided by #PatrickHaugh.
On the other hand, if you are not fond of list comprehensions and some built in functions as zip is, here's a little longer and less elegant version using a for loop:
arr = []
for i in range(0, len(list)):
arr.append(sum(l[i] for l in list) / len(list))
print(arr)
Output:
[1, 4, 4]

Resources