Python:打印没有括号和单引号的列表?

时间:2022-06-07 02:27:23

I have a list full of IP addresses. I would like to iterate through the list and print each IP address. When I try doing this:

我有一个充满IP地址的列表。我想遍历列表并打印每个IP地址。当我尝试这样做时:

def printList(theList):
    for item in theList:
        print item

And the output looks like this:

输出看起来像这样:

['8.0.226.5']
['8.0.247.5']
['8.0.247.71']
['8.0.249.28']
['8.0.249.29']

I have tried everything, including "print item[0]" in the loop. What am I doing wrong?

我已经尝试了一切,包括循环中的“print item [0]”。我究竟做错了什么?

2 个解决方案

#1


6  

Each item in the list is itself a singleton list. There's propably no reason for this - if you can't name one, go and remove them (by using re.find over re.findall or returning a single item from the list returned by re.findall), they're just redundant and cause trouble like in this case.

列表中的每个项目本身都是一个单例列表。可能没有理由 - 如果你不能命名一个,去除它们(通过使用re.find over re.findall或从re.findall返回的列表中返回一个项目),它们只是多余的,在这种情况下造成麻烦。

Regardless, print item[0] should work as it's printing the single element in the list, and unlike the str() of lists, it won't run the item through repr first (which causes the quotes and would escape unprintable characters if there were any in the string). And once you got rid of the redundant singleton lists, print '\n'.join(items) will work as well.

无论如何,打印项[0]应该工作,因为它打印列表中的单个元素,并且与列表的str()不同,它不会首先通过repr运行项目(这会导致引号并且如果存在则会转义不可打印的字符是字符串中的任何一个)。一旦你摆脱了冗余的单例列表,打印'\ n'。join(items)也会起作用。

Your code throws an error if there is an empty list in theList. If there is a line in recentFile that does not contain anything formatted like an IP, an empty list will be returned by returnIP, and if any line in comparisonFile (by the way: you open it with a descriptive name at the beginning, but open it again and again without a descriptive name in chechMatch) contains no IP address either, you'll get another empty list which of course equals the empty list passed as parameter ip. So for non-IP names in recentFile, empty lists will be added. This whole troubel can be avoided if you return strings instead of singleton lists from returnIP, use None when there is no IP in the current line, and skip the checking/appending in compareFiles if returnIP returns None.

如果列表中有空列表,则代码会引发错误。如果recentFile中有一行不包含任何格式化的IP,则returnIP将返回一个空列表,如果compareFile中有任何行(顺便说一句:你在开头用一个描述性名称打开它,但是打开它一次又一次没有chechMatch中的描述性名称也没有包含IP地址,你会得到另一个空列表,当然等于作为参数ip传递的空列表。因此,对于recentFile中的非IP名称,将添加空列表。如果从returnIP返回字符串而不是单例列表,则可以避免整个麻烦,当前行中没有IP时使用None,如果returnIP返回None,则跳过compareFiles中的检查/追加。

#2


0  

I think theList is not a list of IPs but a list of lists of IPs(each of them with 1 element).

我认为该列表不是IP列表,而是IP列表列表(每个列表包含1个元素)。

Another cause of the problem would be that you have an IP class with an overwritten str method that prints it like that.

问题的另一个原因是你有一个带有覆盖str方法的IP类,就像那样打印它。

#1


6  

Each item in the list is itself a singleton list. There's propably no reason for this - if you can't name one, go and remove them (by using re.find over re.findall or returning a single item from the list returned by re.findall), they're just redundant and cause trouble like in this case.

列表中的每个项目本身都是一个单例列表。可能没有理由 - 如果你不能命名一个,去除它们(通过使用re.find over re.findall或从re.findall返回的列表中返回一个项目),它们只是多余的,在这种情况下造成麻烦。

Regardless, print item[0] should work as it's printing the single element in the list, and unlike the str() of lists, it won't run the item through repr first (which causes the quotes and would escape unprintable characters if there were any in the string). And once you got rid of the redundant singleton lists, print '\n'.join(items) will work as well.

无论如何,打印项[0]应该工作,因为它打印列表中的单个元素,并且与列表的str()不同,它不会首先通过repr运行项目(这会导致引号并且如果存在则会转义不可打印的字符是字符串中的任何一个)。一旦你摆脱了冗余的单例列表,打印'\ n'。join(items)也会起作用。

Your code throws an error if there is an empty list in theList. If there is a line in recentFile that does not contain anything formatted like an IP, an empty list will be returned by returnIP, and if any line in comparisonFile (by the way: you open it with a descriptive name at the beginning, but open it again and again without a descriptive name in chechMatch) contains no IP address either, you'll get another empty list which of course equals the empty list passed as parameter ip. So for non-IP names in recentFile, empty lists will be added. This whole troubel can be avoided if you return strings instead of singleton lists from returnIP, use None when there is no IP in the current line, and skip the checking/appending in compareFiles if returnIP returns None.

如果列表中有空列表,则代码会引发错误。如果recentFile中有一行不包含任何格式化的IP,则returnIP将返回一个空列表,如果compareFile中有任何行(顺便说一句:你在开头用一个描述性名称打开它,但是打开它一次又一次没有chechMatch中的描述性名称也没有包含IP地址,你会得到另一个空列表,当然等于作为参数ip传递的空列表。因此,对于recentFile中的非IP名称,将添加空列表。如果从returnIP返回字符串而不是单例列表,则可以避免整个麻烦,当前行中没有IP时使用None,如果returnIP返回None,则跳过compareFiles中的检查/追加。

#2


0  

I think theList is not a list of IPs but a list of lists of IPs(each of them with 1 element).

我认为该列表不是IP列表,而是IP列表列表(每个列表包含1个元素)。

Another cause of the problem would be that you have an IP class with an overwritten str method that prints it like that.

问题的另一个原因是你有一个带有覆盖str方法的IP类,就像那样打印它。