5
5
5p1ke2020-11-07 13:13:20
Python
5p1ke, 2020-11-07 13:13:20

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)])

How can I implement the transfer of all IDs from the array "arrayID" if I do not know how many elements there are.
Since I'm just learning Python, I wrote such a crutch
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)

My execution is not working :(
Is there a normal solution?
Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Antonio Solo, 2020-11-07
@5p1ke

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])

may need to add type conversion explicitly
InputMediaPhoto(str(id))

I
i1yas, 2020-11-07
@i1yas

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)))

S
Stanislaw, 2020-11-07
@wildkain

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 question

Ask a Question

731 491 924 answers to any question