Answer the question
In order to leave comments, you need to log in
How to create an array from another array?
I want to bulk send pictures via telegram bot
using python or "telebot"
I get an array
arrayID=[id1, id2, id3, idN]
In order to send pictures I need to execute such a request
bot.send_media_group(message.from_user.id, [InputMediaPhoto(ID1), [InputMediaPhoto(ID2)])
ext_array = []
for id in arrayID:
add_to_array = 'InputMediaPhoto(' + str(id) + ')'
array.append(ext_array)
bot.send_media_group(message.from_user.id, ext_array)
Answer the question
In order to leave comments, you need to log in
you pass some string
and you need an object
'InputMediaPhoto(' + str(id) + ')'
InputMediaPhoto(id)
bot.send_media_group(message.from_user.id, [InputMediaPhoto(x) for x in arrayID])
InputMediaPhoto(str(id))
Of course, your solution won't work add_to_array = 'InputMediaPhoto(' + str(id) + ')'
- you're just writing a line here, and there should be a function call. In this case, add_to_array is not used, append is performed on some array in addition.
You need to use the built-in map function:
bot.send_media_group(message.from_user.id, list(map(InputMediaPhoto, arrayID)))
your solution doesn't work because 'InputMediaPhoto(' + str(id) + ')' will return you a string which will not be executed by the interpreter.
to iterate over the elements of an array, use iterators (if you need sequential execution) or .map if you need to get a collection of results in the end.
arrayID=[id1, id2, id3, idN]
bot.send_media_group(message.from_user.id, [ InputMediaPhoto(id) for id in arrayID ])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question