你怎么不打印[]?蟒蛇

时间:2016-02-12 12:06:11

标签: python

当我从main调用子程序时,数组括号打印出来

def hobby(hobbies, correct_hobbies, incorrect_hobbies):

    number = 0

    underscore = ["_"]*len(hobbies)

    while number <len(hobbies):

        if hobbies[count] in correct_hobbies:
            underscore[count]=hobbies[count]

2 个答案:

答案 0 :(得分:1)

如果您有一个字符串列表,可以使用join将它们打印为单个字符串而不是列表

>>> l = ['t','h','i','s']
>>> l
['t', 'h', 'i', 's']
>>> ''.join(l)
'this'

如果您想要空格(或任何其他分隔符),请在join

之前的引号中添加空格
>>> l = ['some', 'list', 'of', 'words']
>>> l
['some', 'list', 'of', 'words']
>>> ' '.join(l)
'some list of words'

答案 1 :(得分:-2)

要打印list,但没有括号,您可以使用str.strip()

>>> mylist = [4, 5, 6, 7]
>>> print(str(mylist).strip("[]"))
4, 5, 6, 7

如果您还想删除逗号,则可以改为使用str.join()

>>> mylist = ['4', '5', '6', '7']
>>> print("".join(mylist))
4567

这将适用于您的情况,但需要注意的是,如果mylist中的任何内容不是字符串,则会出现TypeError。