简体   繁体   English

如何拆分列表并将其打印在单独的行中?

[英]how can I split a list and print it in separate lines?

Hi this is my code for Telegram bot,I have a list that fills by names. 嗨,这是我的Telegram机器人代码,我有一个按名称填充的列表。 the bot add usernames to list and reply it as a list I want to reply every name in seperate lines 机器人将用户名添加到列表中并作为列表进行回复我想在单独的行中回复每个名称

list = []
txt = str(list)


def msg_filter(bot , update):
wordsp = ["man"," miam"]
wordsn = ['nemiam','nistam','na']

if len(list) <=15:

    if any (i in update.message.text for i in wordsp):

        if "{}".format(update.message.from_user.first_name) not in list:
            list.append("{}".format(update.message.from_user.first_name))
            bot.send_message(chat_id = update.message.chat_id , text = "{}".format(list))

I have this in output : ['username1','username2','username3'] 我在输出中有这个: ['username1','username2','username3']

but expect this: 但是期望这样:

[username
usernam2
username3]

you could use: 您可以使用:

 print('\n'.join(list))
 # please do not name your list "list", list is a build-in function !!!

文本= “[{}]”。格式( “\\ n” 个。加入(LST))

If you want to output the list new-line separated you can use #join and use the newline-character ( \\n ) as separator for your list entries. 如果要输出以换行符分隔的列表,则可以使用#join并将换行符( \\n )用作列表条目的分隔符。

For example: 例如:

bot.send_message(chat_id = update.message.chat_id , text = "\n".join(list))

which will output: 将输出:

username1
username2
username3

If you explicitly want the [ & ] outputed as well, you can use format to add them: 如果您还明确希望输出[] ,则可以使用format添加它们:

text = "[\n{}\n]".format("\n".join(list))
bot.send_message(chat_id = update.message.chat_id , text = text)

If you need exact this result without any quotes: 如果您需要确切的结果而不带引号:

    [username1
    usernam2
    username3]

Try this simple solution: 试试这个简单的解决方案:

    print(f'[{list_[0]}')
    for l in list_[1:-2]:
        print(l)
    print(f'{list_[-1]}]')

where list_ = ['username1','username2','username3'] 其中list_ = ['username1','username2','username3']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不同的行上打印每个字母两次? - How can I print each letter twice on separate lines? Python:如何从列表中打印单独的行? - python: how to print separate lines from a list? 如何将此包含字典的列表拆分为单独的列? - How can I split out this list containing a dictionary into separate columns? 在单独的行中打印列表列表 - Print list of lists in separate lines Python打印列表在单独的行上 - Python Print list on separate lines 如何打印由数字Python列表指定的文件行? - how can I print lines of a file that specefied by a list of numbers Python? Python:如何使用正则表达式将句子拆分为新行,然后使用空格将标点符号与单词分开? - Python: How can I use a regex to split sentences to new lines, and then separate punctuation from words using whitespace? 如何定义 function 将字符串拆分为单独的行? - How do I define a function to split a string into separate lines? 如何将列表中的所有值放在单独的行中的列表框中? 蟒蛇 - How can I put all the values in the list into a list box on separate lines? Python 如何根据第三个列表在不同行上按不同列表中的数字打印列表中的字符串? - How can I print strings in a list by the number in a different list, on different lines depending on a third list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM