How to convert Tuple of array of Array<(key: String, value: Array<Chat>)> to Dictionary of arrays Dictionary<String, Array<Chat>>?

Related searches

I have my data in the form

Array<(key: String, value: Array<Chat>)>

I want to convert it into

Dictionary<String, Array<Chat>>

where Chat is an array.

Here is my data :-

[(key: "2020-01-27", value: [Unilife.Chat(id: Optional(840), roomID: Optional("5010682260"), message: Optional("Hello"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-27 10:09:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-27"), createdAt: Optional("2020-01-27T10:09:31.000Z"), updatedAt: Optional("2020-01-27T10:09:44.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(841), roomID: Optional("5010682260"), message: Optional("Kjjhhbbb"), thumb: nil, filepath: nil, senderID: Optional(777), receiverID: Optional(658), groupID: nil, chatID: nil, date: Optional("2020-01-27 10:09:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-27"), createdAt: Optional("2020-01-27T10:09:44.000Z"), updatedAt: Optional("2020-01-27T11:02:07.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0)]), (key: "2020-01-28", value: [Unilife.Chat(id: Optional(842), roomID: Optional("5010682260"), message: Optional("Unilife158029258648577759581565958156.m4a"), thumb: Optional(""), filepath: Optional(""), senderID: Optional(777), receiverID: Optional(658), groupID: nil, chatID: nil, date: Optional("2020-01-28 10:09:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("audio"), onlyDate: Optional("2020-01-28"), createdAt: Optional("2020-01-28T10:09:49.000Z"), updatedAt: Optional("2020-01-28T11:02:07.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(843), roomID: Optional("5010682260"), message: Optional("Hee"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-28 11:02:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-28"), createdAt: Optional("2020-01-28T11:02:07.000Z"), updatedAt: Optional("2020-01-28T11:22:21.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0)]), (key: "2020-01-29", value: [Unilife.Chat(id: Optional(844), roomID: Optional("5010682260"), message: Optional("Dasdasd"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-29 12:10:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-29"), createdAt: Optional("2020-01-29T12:10:32.000Z"), updatedAt: Optional("2020-01-29T12:10:43.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(848), roomID: Optional("5010682260"), message: Optional("Hgchchc"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-29 12:11:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-29"), createdAt: Optional("2020-01-29T12:11:34.000Z"), updatedAt: Optional("2020-01-29T12:11:38.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(849), roomID: Optional("5010682260"), message: Optional("Jjjhvjhv"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-29 12:11:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-29"), createdAt: Optional("2020-01-29T12:11:45.000Z"), updatedAt: Optional("2020-01-29T12:12:18.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(850), roomID: Optional("5010682260"), message: Optional("Rr"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-29 12:11:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-29"), createdAt: Optional("2020-01-29T12:11:50.000Z"), updatedAt: Optional("2020-01-29T12:12:18.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(851), roomID: Optional("5010682260"), message: Optional("Hello"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-29 12:12:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-29"), createdAt: Optional("2020-01-29T12:12:14.000Z"), updatedAt: Optional("2020-01-29T12:12:18.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(852), roomID: Optional("5010682260"), message: Optional("H"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-29 12:28:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-29"), createdAt: Optional("2020-01-29T12:28:39.000Z"), updatedAt: Optional("2020-01-29T12:30:32.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0)]), (key: "2020-01-30", value: [Unilife.Chat(id: Optional(853), roomID: Optional("5010682260"), message: Optional("Jhffjh"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-30 12:28:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-30"), createdAt: Optional("2020-01-30T12:28:45.000Z"), updatedAt: Optional("2020-01-30T12:30:32.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0), Unilife.Chat(id: Optional(855), roomID: Optional("5010682260"), message: Optional("Svsvvsvs"), thumb: nil, filepath: nil, senderID: Optional(658), receiverID: Optional(777), groupID: nil, chatID: nil, date: Optional("2020-01-30 12:49:00"), seen: Optional("1"), isDeleted: Optional("00"), deleteChatID: Optional("0"), messageType: Optional("text"), onlyDate: Optional("2020-01-30"), createdAt: Optional("2020-01-30T12:49:42.000Z"), updatedAt: Optional("2020-01-30T12:50:11.000Z"), senderUserChat: nil, chatSlide: nil, isPlaying: false, currentDuration: 0.0)])]

Simply map the array, however as both collection types are not related you have to use a loop or forEach

var dictionary = [String:[Chat]]()
array.forEach{ dictionary[$0.key] = $0.value }

Python convert tuple to array, Using list comprehension: >>> a = [] >>> a.append((1,2,4)) >>> a.append((2,3,4)) >>> [x for xs in a for x in xs] [1, 2, 4, 2, 3, 4]. How to convert List or Tuple into NumPy array? The array () function can accept lists, tuples and other numpy.ndarray objects also to create new array object. import numpy as np thelist = [1, 2, 3] print(type( thelist)) # <class 'list'> array1 = np. array( thelist) print(type( array1)) # <class 'numpy.ndarray'> thetuple = ((1, 2, 3)) print(type( thetuple)) # <class 'tuple'> array2 = np. array( thetuple) print(type( array2)) # <class 'numpy.ndarray'> array3 = np. array([ thetuple, thelist,

You can use the init(uniqueKeysAndValues:) initialiser of Dictionary if you are sure that the keys are unique:

Dictionary(uniqueKeysWithValues: yourArrayOfTuples)

If you are not sure that the keys are unique, you can use init(keysAndValues:uniquingKeysWith:) and provide a function to handle duplicate keys:

Dictionary(yourArrayOfTuples, uniquingKeysWith: {
    value1, value2 in
    // return which value you would like
})

How can I convert a Python tuple to an Array?, To convert a tuple to an array(list) you can directly use the list constructor. examplex = (1, 2, 3) y = list(x) print(y)OutputThis will give the output� NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to convert a list and tuple into arrays.

You can use reduce, if a key exists for multiple tuples the corresponding arrays gets merged.

let dict = array.reduce(into: [:]) {
    $0[$1.key, default: []].append(contentsOf: $1.value)
}

If you on the other hand knows that all keys are unique you can simplify the reduce to

let dict = array.reduce(into: [:]) {
    $0[$1.key] = $1.value
}

numpy.asarray() in Python, asarray() function is used when we want to convert input to an array. Input can be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and� If you're passing your "array" (list) to a function that wants an iterable, virtually all of them will happily take a list, tuple, numpy.array, yourawesomeiterabletype, whatever. – Nick T Sep 10 '10 at 19:40

Python, Given below are various methods to convert numpy array into tuples. Method #1: Using tuple and map. filter_none. edit close. play_arrow. link Converting List into array in Java Por Yashar Ahmadov 1 Comentarios Visto 65 veces java arrays list tuples anylogic. COMENTARIOS. Yashar Ahmadov .

numpy.asarray — NumPy v1.19 Manual, Convert the input to an array. Parameters. aarray_like. Input data, in any form that can be converted to an array. This includes lists, lists of tuples� The command numpy.asarray will turn a number of pre-set iterable containers (list, tuple, etc) into a numpy array. Tags arrays, numpy, python, scipy, tuples Not what you're looking for? Browse other questions tagged arrays, numpy, python, scipy, tuples

How can I convert a Python tuple to an Array? How to convert an object array to an integer array in Java? How do I check if an array includes an object in JavaScript? Convert object to an array in PHP. JavaScript Convert an array to JSON; How to create an array of strings in JavaScript? How to convert an array into JavaScript string?