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

发布于 2024-11-02 22:24:59 字数 308 浏览 0 评论 0原文

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

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

输出如下所示:

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

我已经尝试了所有方法,包括循环中的“print item[0]”。我做错了什么?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2024-11-09 22:24:59

列表中的每个项目本身就是一个单例列表。可能没有理由这样做 - 如果您不能说出其中一个,请去删除它们(通过使用 re.find 而不是 re.findall 或从由 re.findall 返回的列表),它们只是多余的并且会造成像本例这样的麻烦。

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

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

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.

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.

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.

挥剑断情 2024-11-09 22:24:59

我认为 theList 不是 IP 列表,而是 IP 列表的列表(每个 IP 都有 1 个元素)。

问题的另一个原因是您的 IP 类具有被覆盖的 str 方法,可以像这样打印它。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文