gpt4 book ai didi

Python拆分、替换

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:08 28 4
gpt4 key购买 nike

我正在写一张纸条,遇到了障碍。可能有更有效的方法来执行此操作,但我对 Python 还很陌生。我正在尝试创建用户生成的 IP 地址列表。我正在使用 print 来查看生成的值是否正确。当我运行此代码时,打印 ip_start 具有相同的值并且未更新。我确信这是一个相当简单的修复,但我有一个主要的脑锁。

ip_start = raw_input('Please provide the starting IP address for your scan --> ')
start_list = ip_start.split(".")
ip_end = raw_input('Please provide the ending IP address for your scan --> ')
end_list = ip_end.split(".")
top = int(start_list[3])
bot = int(end_list[3])
octet_range = range(top,bot)
print octet_range
for i in octet_range:
print i
print "This the top:" + str(top)
ip_start.replace(str(top),str(i))
print ip_start

最佳答案

字符串上的replace 方法不会就地修改字符串。事实上,没有就地修改字符串;它们是不可变的。这在教程部分进行了解释 Strings .

它所做的是返回一个新的字符串,并对其进行替换。来自 the docs :

str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

所以,你想要的是:

ip_start = ip_start.replace(str(top),str(i))

关于Python拆分、替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20507139/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com