Python:打印没有括号和单引号的列表?
我有一个完整的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
列表中的每个项目本身就是一个单例列表。可能没有理由这样做 - 如果您不能说出其中一个,请去删除它们(通过使用
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
overre.findall
or returning a single item from the list returned byre.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 thestr()
of lists, it won't run the item throughrepr
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 inrecentFile
that does not contain anything formatted like an IP, an empty list will be returned byreturnIP
, and if any line incomparisonFile
(by the way: you open it with a descriptive name at the beginning, but open it again and again without a descriptive name inchechMatch
) contains no IP address either, you'll get another empty list which of course equals the empty list passed as parameterip
. So for non-IP names inrecentFile
, empty lists will be added. This whole troubel can be avoided if you return strings instead of singleton lists fromreturnIP
, useNone
when there is no IP in the current line, and skip the checking/appending incompareFiles
ifreturnIP
returnsNone
.我认为 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.