Python Forum
Multiplying two lists to make an array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiplying two lists to make an array
#1
How have two lists of unequal length with values in each. I want to multiply these two lists to make an array containing a formula applied to each combination of values

eg

a = [1,2,3,4]
b = [0,1,2]
and I want to make a new array © whose values are derived from the formula c=(a*b)+1

I want to do so in the fastest way possible without using for loops.

The expected array © should be

Output:
array([[1, 1, 1, 1], [2, 3, 4, 5], [3, 5, 7, 9]])
Can anyone help?

Thanks Peter
Reply
#2
You want to have look at this: https://www.w3schools.com/python/python_for_loops.asp
Reply
#3
#!/usr/bin/python3
a_lst = [1,2,3,4]
b_lst = [0,1,2]

# with for-loops
lst = [ ]
for b in b_lst:
    tmp = [ ]
    for a in a_lst:
        tmp += [ a*b+1 ]
    lst += [ tmp ]
print(lst)

# without for-loops
lst = [ [a*b+1 for a in a_lst] for b in b_lst ]
print(lst)
Reply
#4
This is what I did, I got pretty close to your example above -
a = [1,2,3,4]
b = [0,1,2]
c = ([[]])
countb = 0
looping = True

while looping:
    counta = 0
    for let in a:
        try:
            total = a[counta]*b[countb] + 1
            c[countb].append(total)
        except:
            looping = False
        counta += 1
    if looping:
        countb += 1
        c += [[]]
    
print(c)

Doing this without a for loop would require you to count the amount of times you need to repeat, and then it becomes very inefficient with other lists
Reply
#5
Short answer:
import numpy as np
a = np.array([1,2,3,4])
b = np.array([0,1,2])
result = np.outer(b,a)+1
print(result)
Answer to match the syntax you suggested:

import numpy as np

class Two_lists_one_array:
    def __init__(self, my_list):
        self.my_list = my_list
        
    def __mul__(self, other):
        return np.outer(other.my_list, self.my_list)

a = Two_lists_one_array([1,2,3,4])
b = Two_lists_one_array([0,1,2])

result = a * b + 1

print(result)
Both result in the following output:
Output:
[[1 1 1 1] [2 3 4 5] [3 5 7 9]]
Reply
#6
One question many implementations.

# free bugs included
class MList(list):
    def __init__(self, iterable, *, multi=False):
        self.multi = multi
        super().__init__(iterable)
    def __mul__(self, other):
        cls = self.__class__
        return cls([[x*y for y in self] for x in other], multi=True)
    def __add__(self, other):
        cls = self.__class__
        if isinstance(other, cls):
            return cls([[x + y for y in self] for x in other])
        elif self.multi:
            return cls([[y + other for y in x] for x in self])
        else:
            return cls([x + other for x in self])


a = MList([1,2,3,4])
b = MList([0,1,2])

print(a * b + 1)
Output:
[[1, 1, 1, 1], [2, 3, 4, 5], [3, 5, 7, 9]]
No Error checking, no support for more dimensions...
If you do something unexpected, it blows up.
If you implement this all, the code grows fast.

It's good to know nested list comprehensions, as a takeaway.
The rest is a bit playing with magic methods of classes.
So you can change the behavior. Putting the complex stuff into
a class and reuse logically the operators (*,/,+,-,@,%,**,...).
The class MList inherits from class list (which is a built-in type).
All methods, which are not implemented in class MList, are
looked up in the list type. The list implements __len__ (on c level I think).

So, len(a) still works.
Also visible methods like sort, extend, append etc.
are still usable on the MList object. If you extend a
class/type with you own code, you may get some unexpected
behavior.

Doing this to get deeper into Python is not an easy task.
Often functions are simpler, shorter and easier.
Classes are often used in cases, where nobody needs them.
And there are libraries, which have solved most of our problems.
Everything I have shown, is interesting for small lists, toy applications.

If it's getting huge (many dimensions), the approach with lists will be very slow.
Numpy solves this problem for us.

PS: The example changed the API of a list. The + operator was before for list concatenation.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
Normally I would issue warnings for users who provide substantial code for a question that has shown no effort, but since several regulars did it this time, I'm just going to make this post:

Please be mindful to not accidentally do people's homework for them.
Reply
#8
Are you for real? Just because it was posted in "Homework" section doesn't mean that the guy's homework was to do 1 simple operation.

If he posted "what is 2+2?" would you also issue warnings for people who reply with "4"?

Edit: It isn't even in the homework section, so it's as if helping in the "General Coding Help" was forbidden lol.
And what kind of effort you expect from the guy who asks about such simple thing? He described it clearly, showed example of expected output.
Reply
#9
(May-14-2019, 12:08 AM)michalmonday Wrote: Normally I would issue warnings for users who provide substantial code for a question that has shown no effort, but since several regulars did it this time, I'm just going to make this post:

Please be mindful to not accidentally do people's homework for them.
For the record this isn't homework (I have been in the workforce for 34 years) and I have researched various possible solutions such as itertools and list comprehension but my research didn't answer my specific question.

And to the comments by Michalmonday je dis 'merci beaucoup pour votre aide'.

regards
Peter
Reply
#10
Hi pBerrett,

What is now the fastest solution ?
As soon as you will know it,
let us know it too.

heiner88
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do i make a new lists out of an list ozezn1 1 1,683 Oct-28-2020, 10:19 PM
Last Post: Gribouillis
  Multiplying between two values doug2019 7 2,806 Oct-20-2020, 05:49 PM
Last Post: doug2019
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,366 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Make an array of string number in a List polantas 5 3,090 May-27-2020, 07:18 AM
Last Post: buran
  List structure lost when multiplying Protonn 2 2,232 Apr-23-2020, 04:16 AM
Last Post: buran
  multiplying elements in a list Olavv 3 3,413 Feb-27-2020, 04:55 PM
Last Post: DeaD_EyE
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,257 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020