3

I have an array "labels" with three thousand 0 and 1 values. If I multiply this with -1, it results in an empty array. I am not able to understand why it is resulting in 0?

Input

print(labels[0:10])

Output

['0', '1', '1', '0', '1', '0', '0', '1', '0', '0']

Now, if i do:

labels=labels*(-1)
print(labels)

It returns:

[]

I was expecting an array with 0 and -1 values. Please explain.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • 2
    That appears to be a list, rather than an array. – user2357112 Apr 21 '20 at 11:42
  • 1
    Either use list comprehension: `print([int(x) * -1 for x in labels])` or numpy arrays `print(np.array(labels).astype(int)*-1)`. Also have a look [here](https://stackoverflow.com/a/35166717/9758194) – JvdV Apr 21 '20 at 11:50

6 Answers6

4

Operators * and + does not work the same on arrays and lists.

On an array, it computes the operation element-wise. On a list, it repeats or extends the list.

For instance:

a = [1, 2, 3]
print(a * 2)  # prints [1, 2, 3, 1, 2, 3]
print(np.array(a) * 2)  # prints np.array(2, 4, 6)

Hence, whatever the type of its element, a list object multiplied by -1 is the empty list. Convert your list into an array before doing your operation:

labels = np.array(labels).astype(int) # Since you have a list of str
labels = labels * (-1)
print(labels)
Tristan Nemoz
  • 1,844
  • 1
  • 6
  • 19
  • Thank you. Very detailed explanation. How to resolve such doubts on our own? I am very new to programming. – Deepak Dhiman Apr 21 '20 at 11:51
  • There's no magic method other than practicing. Fortunately, you get used to it quickly enough, don't worry! – Tristan Nemoz Apr 21 '20 at 12:05
  • What am i doing wrong here? sent_splt=[['good', 'case,', 'excellent', 'value.'], ['great', 'for', 'the', 'jawbone.'],['tied', 'to', 'charger', 'for', 'conversations', 'lasting', 'more', 'than', '45', 'minutes.major', 'problems!!']] stop_set = ['the', 'a', 'an', 'i', 'he', 'she', 'they', 'to', 'of', 'it', 'from'] x=[a for a in sent_splt if a not in stop_set] print(x) It is not filtering the words. I am trying to filter stop words. – Deepak Dhiman Apr 22 '20 at 11:40
  • I think it is better that you post this as another question to use proper code formatting. – Tristan Nemoz Apr 22 '20 at 13:04
  • Actually, It is saying to post question after 1 day break from last 2 days for me. Basically I am not allowed to post question because of my previous question which was not as per stackoverflow standard. Sorry for the incovenience. – Deepak Dhiman Apr 23 '20 at 06:24
  • 1
    If I understand correctly, it comes from the fact that `sent_splt` and `stop_set` do not contain the same type of object. The former contains `list` objects, the latter `str` objects. Hence, the condition `a not in stop_set` fo `a` being in `sent_splt` is always `True`. – Tristan Nemoz Apr 23 '20 at 12:37
  • Thank you @g2i I think that is the case. Thanks for your reply :) – Deepak Dhiman Apr 23 '20 at 16:04
3

Everything in the list is a string, multiplying a string by -1 is nothing:

print('1'*-1)

Prints nothing. Therefore in your case, the array is empty.

This can be fixed like this:

for i in range (len(labels)):
    labels[i]=int(labels[i])*-1
print(labels)

Here, each of the elements is first converted to an integer before being multiplied.

Or as DerHamm suggested:

labels = [int(labels[i]) * -1 for i in range(len(labels))]
  • FYI: Same solution as comprehension: labels = [int(labels[i]) * -1 for i in range(len(labels))] Increases the performance slightly. – DerHamm Apr 21 '20 at 11:48
  • Thank you. Such a silly question from me! How to resolve such doubts on our own? I am very new to programming. – Deepak Dhiman Apr 21 '20 at 11:49
3

The multiplication operation on lists in python does not apply the operation on each of the elements, but rather the whole list.

l = [1,2,3] * 2 # l == [1,2,3,1,2,3]

Taking this into account: multiplying by 0 would result in an empty list and anything below 0 wouldn't make sense, so it's also just an empty list (I believe this would make sense to raise an exception/valueerror here, but alas, that's not the case today).

What it looks like you want to do is multiply each element by -1:

l = [int(s) * -1 for s in labels]
# with maybe an additional conversion back to strings:
l = map(str, l)
smassey
  • 5,875
  • 24
  • 37
2

Your list has str values. So do this:

In [1945]: l = ['0', '1', '1', '0', '1', '0', '0', '1', '0', '0']
In [1949]: [int(i) *(-1) for i in l]                                                                                                                                                                        
Out[1949]: [0, -1, -1, 0, -1, 0, 0, -1, 0, 0]
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

Convert the string to int and multiply using a list comprehension:

list_of_numbers = ['0', '1', '0', '1']
a = [int(i) * -1 for i in list_of_numbers]
print(a)

result:

[0, -1, 0, -1]
daveg
  • 535
  • 2
  • 10
1

You can't use arithmetic directly on the list and your list apparantely contains strings so try this:

test = ['0', '1', '1', '0', '1', '0', '0', '1', '0', '0']
test = [x*-1 for x in list(map(int, test))]
test

[0, -1, -1, 0, -1, 0, 0, -1, 0, 0]
hamid.khb
  • 432
  • 2
  • 11