gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
python list of only unique entries in a multidimensional array
- User
python list of only unique entries in a multidimensional array
- Assistant
To get a list of only unique entries in a multidimensional array in Python, you can use a combination of list comprehension and set. Here's an example:
# Input multidimensional array array = [[1, 2, 3], [4, 5, 6], [1, 2, 3], [7, 8, 9], [4, 5, 6]] # Flatten the multidimensional array into a single list flattened_array = [item for sublist in array for item in sublist] # Use set to get unique entries and convert back to list unique_entries = list(set(flattened_array)) print(unique_entries)Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]In this example, the
arrayvariable represents your multidimensional array. Then, theflattened_arrayvariable is created using list comprehension to iterate over each sublist and item in the array. Finally, we convert the flattened array into a set, which automatically removes any duplicate entries, and then convert it back to a list using thelist()function.