-
Question on Home>"Split List"
I'm trying to solve "Split List". I'm using this function to complete the task:
import numpy as np def split_list(items: list) -> list: # your code here arr = np.array_split(items, 2) return np.array(arr).tolist() if __name__ == '__main__': print("Example:") print(split_list([1, 2, 3, 4, 5, 6])) ...
And this returns the expected value, which is:
Example: [[1, 2, 3], [4, 5, 6]] <MYCODE>:5: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <module>, 14
However, when I modify the Example:
import numpy as np def split_list(items: list) -> list: # your code here arr = np.array_split(items, 2) return np.array(arr).tolist() if __name__ == '__main__': print("Example:") print(split_list([1, 2, 3]))
It instead returns:
Example: [array([1, 2]), array([3])] <MYCODE>:5: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <module>, 14
Why does my code only return the "array(...)" part when it hits that specific example? Is there any way I can modify my code in order to strip it from the return value of the function?
I might find a different solution in the meantime.
Edit: tagging appropriately
Edit 2: Just for clarity, if I modify my code to just return that "arr" variable, then I'm not even able to pass the first assert() statement in the code since it keeps adding the array part:
import numpy as np def split_list(items: list) -> list: # your code here arr = np.array_split(items, 2) return arr if __name__ == '__main__': print("Example:") print(split_list([1, 2, 3, 4, 5, 6]))
Example: [array([1, 2, 3]), array([4, 5, 6])] ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <module>, 13
AND if I try turning arr back into a numpy array:
import numpy as np def split_list(items: list) -> list: # your code here arr = np.array_split(items, 2) return np.array(arr) if __name__ == '__main__': print("Example:") print(split_list([1, 2, 3, 4, 5, 6]))
Then it doesn't even format it the way that the problem is asking for:
Example: [[1 2 3] [4 5 6]] ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <module>, 13
edit 3: So effectively, I have to make it a list using the "tolist()" method, but if it doesn't return the same for every example, I'm not sure what to do.
edit 4: UGGHH and when I modify the example to:
import numpy as np def split_list(items: list) -> list: # your code here arr = np.array_split(items, 2) return np.array(arr).tolist() if __name__ == '__main__': print("Example:") print(split_list([1, 2, 3, 4]))
it returns correctly:
Example: [[1, 2], [3, 4]] <MYCODE>:5: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <module>, 14
So I guess it only likes arrays with even length...
edit 5: I know I'm probably spamming with all of these updates, I'm kinda using this to puzzle at this in my head
I figured out how to at least remove the VisibleDeprecationWarning by figuring out what the message pertains to, but it's still doing the same thing:
import numpy as np def split_list(items: list) -> list: # your code here arr = np.array_split(items, 2) newarr = np.array(arr, dtype=object).tolist() return newarr if __name__ == '__main__': print("Example:") print(split_list([1, 2, 3]))
Example: [array([1, 2]), array([3])] ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() <module>, 14
The question is resolved.