关于python:For循环生成序列

For Loop generate sequence

本问题已经有最佳答案,请猛点这里访问。

需要帮助,

我想输出是这样的

1
2
3
4
1.1.1.1 is server0
1.1.1.2 is server1
1.1.1.3 is server2
1.1.1.4 is server3

但我得不到我所期望的输出。这是我的密码

1
2
3
4
IP = ['1.1.1.1', '1.1.1.2', '1.1.1.3', '1.1.1.4']
for ip in IP:
    for i in range(len(IP)):
        print(ip + ' is ' + 'server'+ str(i))

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1.1.1.1 is server0
1.1.1.1 is server1
1.1.1.1 is server2
1.1.1.1 is server3
1.1.1.2 is server0
1.1.1.2 is server1
1.1.1.2 is server2
1.1.1.2 is server3
1.1.1.3 is server0
1.1.1.3 is server1
1.1.1.3 is server2
1.1.1.3 is server3
1.1.1.4 is server0
1.1.1.4 is server1
1.1.1.4 is server2
1.1.1.4 is server3

有人能帮我吗?


这里不需要两个回路。简单写:

1
2
for i in range(len(IP)):
            print(IP[i] + ' is ' + 'server'+ str(i))

输出:

1
2
3
4
1.1.1.1 is server0
1.1.1.2 is server1
1.1.1.3 is server2
1.1.1.4 is server3