0

How would I get these lists to pass through my function (is that the correct terminiology)?

Lists:

temp_a = [1, 2, 3, 4, 5]
temp_b = [2, 2, 3, 4, 1]
temp_c = [1, 0, 2, 2, 1]

Function (I have just put in the * here, but I want to define the list in the function):

def temp_class(temperature):
temp_class = []
for i in *:
    if i < 3:
        temp_class.append(0)
    else:
        temp_class.append(1)

Example that doesn't work:

temp_value = [tempclass(t) for t in temp_a] #initial function is wrong
print(temp_value)

What is wrong here? I have tried to look on Stack Overflow and elsewhere, but can't find anything. Maybe I am using the wrong terminology.

What do I use instead of the *?

william3031
  • 1,653
  • 1
  • 18
  • 39
  • `for i in temp_a:` to iterate `temp_a`. `for i in temp_a + temp_b + temp_c:` to iterate all three of them one after another. Also, just search 'python iterate array' on google, you'll be surprised of what you find – Maor Refaeli Oct 14 '18 at 08:19
  • Do you mean simply processing the lists in your function? Then you simply can define parameters for that: `def temp_class(list1, list2, list3):` and then you have them available in your function. – colidyre Oct 14 '18 at 08:20
  • @MaorRefaeli but that bit is in my function. How do I put something generic in there that is called by the function such as in the example? – william3031 Oct 14 '18 at 08:22
  • @colidyre, would I define them in my function though? Or do define them when I call the function. I want it to work with any list of numbers. – william3031 Oct 14 '18 at 08:24
  • You already iterating the array inside your function. just pass the array `temp_value = temp_class(temp_a)` – Maor Refaeli Oct 14 '18 at 08:25
  • @MaorRefaeli, thanks. I can do that, but the function doesn't work at the moment. What do i put in here `def temp_class(temperature): temp_class = [] for i in *:` I don't want to write a new function each time. – william3031 Oct 14 '18 at 08:26
  • `for i in temperature:` will iterate through the list that you use in your method call – Wazaki Oct 14 '18 at 08:26
  • @OsumanAAA I did that, with the change that was suggested above, but got 'None' as the output. – william3031 Oct 14 '18 at 08:28
  • @williaml you have to define it in the function header/signature. If you have a uncertain number of lists you want to add to your function, then [unpacking operator (*)](https://www.python.org/dev/peps/pep-0448/) is your friend: `def temp_class(*args)` – colidyre Oct 14 '18 at 08:30
  • @williaml the function probably not working because you are missing the `return`statement. Check my answer for an alternative – Maor Refaeli Oct 14 '18 at 08:32

4 Answers4

1

I gues you are looking for the asteriks arguments:

def foo(*args, **kwargs):
    print(args)
    print(kwargs)

test:

foo([1,2,3], bar1="foobar", bar2="foobar2" )

([1, 2, 3],)
{'bar1': 'foobar', 'bar2': 'foobar2'}
return42
  • 543
  • 3
  • 10
1

Maybe this will be more readable (foo and bar return the exact same result):

a = [1, 2, 3, 4, 5]

# With map+lambda
def foo(array):
    return map(lambda x: 0 if x < 3 else 1, array)

# With list comprehension
def bar(array):
    return [0 if x < 3 else 1 for x in array]

print(foo(a))  # [0, 0, 1, 1, 1]
print(bar(a))  # [0, 0, 1, 1, 1]
Maor Refaeli
  • 2,417
  • 2
  • 19
  • 33
  • absolutely not :) , regarding more readable , nicer coding - for sure. – ddor254 Oct 14 '18 at 08:33
  • Yes. This is it. Thanks. – william3031 Oct 14 '18 at 08:35
  • @ddor254 Really? This is pretty straightforward to me. What makes it less readable? (IMO the logic is simple enough to use lambda on a one-liner) – Maor Refaeli Oct 14 '18 at 08:36
  • 1
    You can argue if it's more readable or not. Many python programmers do prefer list comprehensions over map(lambda). Imho you should use list comprehensions for that. See also a [huge discussion on SO](https://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map) for that. – colidyre Oct 14 '18 at 08:37
  • OK i can dig that, I added an alternative with list comprehension – Maor Refaeli Oct 14 '18 at 08:39
  • @MaorRefaeli I understood that right away, but for a starter python programmer the `lambda` is a little confusing (from what i encountered). Regardless as @colidyre mentioned list comprehensions is more preferable. And i did not meant to say it will not work or it is not nicer code, jsut pointed out that for new Python programmers this might see confusing. – ddor254 Oct 14 '18 at 08:50
0

I think I understood what you want to do and here is how you do it:

all list = [temp_a,temp_b,temp_c]
temp_list = []
def temp_class(temperature):
    for i in temperature:
        if i < 3:
            temp_class.append(0)
        else:
            temp_class.append(1)

and then just call your function like so:

[tempclass(t) for t in all_list]

print temp_list

ddor254
  • 1,570
  • 1
  • 12
  • 28
  • Thanks. This didn't quite work for me, but maybe I did it incorrectly. Also, I didn't need all the lists, but good. – william3031 Oct 14 '18 at 08:36
0

It looks like you are trying to map each of your temp lists to their equivalent class lists, in which case it would be a good idea to pass the temp list itself as an argument to the function:

def temp_class(temp_list):
    temp_class = []
    for i in temp_list:
        if i < 3:
            temp_class.append(0)
        else:
            temp_class.append(1)
    return temp_class

If you want to have a function that accomplishes the same thing in a cleaner way (same as Maor's approach), you can do:

def temp_class(temp_list):
    return list(map(lambda t: 0 if t < 3 else 1, temp_list))

In this way, you will be able to get the class mapping of temp_a, for example, in this way:

print(temp_a)
>>>[1, 2, 3, 4, 5]

temp_a_class = temp_class(temp_a)

print(temp_a_class)
>>>[0, 0, 1, 1, 1]

and the class mapping for temp_b in this way:

print(temp_b)
>>>[2, 2, 3, 4, 1]

temp_b_class = temp_class(temp_b)

print(temp_b_class)
>>>[0, 0, 1, 1, 0]

and so on.

haxtar
  • 1,962
  • 3
  • 20
  • 44
  • No problem! Yes, this should accomplish the same thing as Maor’s function you tried. If you’re looking for a direct modification of your original code, see my first function. If you are looking to simplify the function, see the second modification. Either way, they will create the mapping you desire! – haxtar Oct 14 '18 at 08:45