
[英]How do I invert my DataFrame (the first value --> last value, the last value --> first value)
[英]How do I print the first and last value?
下面的部分代码显示了子网中的所有主机IP地址,我想修改代码,使其仅打印此列表的起始地址和最后一个地址
如何在此处使用数组打印第一个和最后一个值?
import ipaddress
print('enter subnet') # in CIDR Format
x = input()
IP= ipaddress.ip_network(x, strict = False)
for y in IP.hosts():
print(y)
电流输出
enter subnet
192.0.0.0/29
192.0.0.1
192.0.0.2
192.0.0.3
192.0.0.4
192.0.0.5
192.0.0.6
所需的输出
HostMin: 192.0.0.1
HostMax: 192.0.0.6
========================================
使用列表后,我可以打印第一个和最后一个值
however this takes quite longer to compute whenever i give a large
subnet
like 192.0.0.0/8 takes longer to print the first and last value,
for: IPV6 address calculations it hangs forever,
for: example: the IPV6 address is 2001:db8::1/96
this list will have 4294967294 elements since this IPV6 subnet has
these many IP address and it hangs forever to print the first and
last element of the list
list[0]
和list[-1]
分别为您提供第一个和最后一个元素
import ipaddress
print('enter subnet') # in CIDR Format
x = input()
IP= ipaddress.ip_network(x, strict = False)
k = list(IP.hosts())
print("HostMin: ",k[0])
print("HostMax: ",k[-1])
更新了答案,以获取第一个和最后一个IP,而不生成整个IP范围
import ipaddress
def hosts(IPTYPE):
"""Custom function derived from IPv6Network/IPv4Network.hosts to get only first and last host
"""
network = int(IPTYPE.network_address)
broadcast = int(IPTYPE.broadcast_address)
return IPTYPE._address_class(network+1),IPTYPE._address_class(broadcast)
print('enter subnet') # in CIDR Format
x = input()
IP= ipaddress.ip_network(x, strict = False)
m = hosts(IP)
print("HostMin: ",m[0])
print("HostMax: ",m[1])
替换for y in IP.hosts():
的y = list(IP.hosts())
for y in IP.hosts():
通过y = list(IP.hosts())
,然后可以执行
print y[0]
print y[-1]
您应该阅读hosts()函数的文档“通过网络中的可用主机返回迭代器”
您在这里不使用数组。 使用清单!
first_item = list[0]
last_item = list[-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.