Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于变量分配字符串的循环_Python_Loops_For Loop_Iterator_Iteration - Fatal编程技术网

Python 基于变量分配字符串的循环

Python 基于变量分配字符串的循环,python,loops,for-loop,iterator,iteration,Python,Loops,For Loop,Iterator,Iteration,我有多个IP地址,范围从xxx.xx.xxx.11到xxx.xx.xxx.50。 对于IPAddress.11,我想将字符串“A01”分配给变量cabine。 要使用IP地址.12,字符串“A02”等直到.30(“A20”) 然后,从IPAddress.31,我想将字符串“B01”,.32“B02”等一直添加到.50“B20” 虽然我是一个绝对的初学者,我认为这不会那么难,但它只是不想工作 我想用for循环编程,类似这样: for (i = 11; i < 51; i++) { cab

我有多个IP地址,范围从
xxx.xx.xxx.11
xxx.xx.xxx.50
。 对于IPAddress
.11
,我想将字符串
“A01”
分配给变量
cabine
。 要使用IP地址
.12
,字符串
“A02”
等直到
.30
“A20”

然后,从IPAddress
.31
,我想将字符串
“B01”
.32
“B02”
等一直添加到
.50
“B20”

虽然我是一个绝对的初学者,我认为这不会那么难,但它只是不想工作

我想用for循环编程,类似这样:

for (i = 11; i < 51; i++) {
  cabine = A and something with i;
}
(i=11;i<51;i++)的
{
cabine=A和i;
}
但我需要两个不同的循环,因为字母(A,B)不同,对吗?
提前感谢您的帮助

使用
if
语句确定前缀
A
B
以及循环索引如何转换为后缀号。然后使用格式化运算符添加前导零(请参阅)

范围(11,51)内的i的

如果我以下是您想要的示例:

cabine=[]
数据=[“xxx.xx.xxx.11”、“xxx.xx.xxx.24”、“xxx.xx.xxx.34”、“xxx.xx.xxx.45”、“xxx.xx.xxx.49”、“xxx.xx.xxx.50”]
对于数据中的val:
number=int(val[-2:])

如果(数字>10、数字30和数字用于处理您的IP地址,您可以使用
ipaddress
模块。
.packeted
成员可以访问
IPV4Address
中的每个数字。然后您需要一个将IP地址转换为a/B和所需数字的公式

from ipaddress import IPv4Address

def gen_name(ip):
    i = IPv4Address(ip).packed[3]
    return f"{'A' if i < 31 else 'B'}{(i-11)%20+1:02}"

for i in range(11,51):
    ip = f'192.168.1.{i}' # generate IPs for testing
    name = gen_name(ip)
    print(ip,name)

这甚至不是Python循环的语法,看起来像是C.Python对范围(11,51)中的i使用
抱歉,我弄错了。我想用python编程。我的意思是我已经有了一个通过if循环的解决方案,但这一点听起来并不合理,但我如何将IP地址和I组合在一起?因为我通过一个程序获得IP地址。它将IP地址存储在另一个名为IPAddress的变量中。我真的不知道last两位数字可以转换成这个i变量
IPAddress=f'xxx.xxx.xxx.{i}'
,看起来很好!但是我的IPAddress存储在另一个名为IPAddress的变量中。我用一些python代码得到了它。是的,你应该编辑你的问题以使其易懂。我制作了一个“示例”,你可以在你的代码中实现它。
from ipaddress import IPv4Address

def gen_name(ip):
    i = IPv4Address(ip).packed[3]
    return f"{'A' if i < 31 else 'B'}{(i-11)%20+1:02}"

for i in range(11,51):
    ip = f'192.168.1.{i}' # generate IPs for testing
    name = gen_name(ip)
    print(ip,name)
192.168.1.11 A01
192.168.1.12 A02
192.168.1.13 A03
192.168.1.14 A04
192.168.1.15 A05
192.168.1.16 A06
192.168.1.17 A07
192.168.1.18 A08
192.168.1.19 A09
192.168.1.20 A10
192.168.1.21 A11
192.168.1.22 A12
192.168.1.23 A13
192.168.1.24 A14
192.168.1.25 A15
192.168.1.26 A16
192.168.1.27 A17
192.168.1.28 A18
192.168.1.29 A19
192.168.1.30 A20
192.168.1.31 B01
192.168.1.32 B02
192.168.1.33 B03
192.168.1.34 B04
192.168.1.35 B05
192.168.1.36 B06
192.168.1.37 B07
192.168.1.38 B08
192.168.1.39 B09
192.168.1.40 B10
192.168.1.41 B11
192.168.1.42 B12
192.168.1.43 B13
192.168.1.44 B14
192.168.1.45 B15
192.168.1.46 B16
192.168.1.47 B17
192.168.1.48 B18
192.168.1.49 B19
192.168.1.50 B20