Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
字符串元素计数不';t工作[Python]_Python_Python 3.x_List_Count - Fatal编程技术网

字符串元素计数不';t工作[Python]

字符串元素计数不';t工作[Python],python,python-3.x,list,count,Python,Python 3.x,List,Count,我正在编写一个简单的服务器套接字接口(如控制台)。我试图实现一个SEND to xxx.xxx.xxx.xxx命令,在ip地址xxx.xxx.xxx向客户端发送一些信息或命令。所以我试着写这段代码(它在一个类中,这就是为什么出现self参数的原因): 这是控制台: [Server] >>> SEND TO 111.111.1.11 0 ['111.111.1.11', '111.111.1.11'] 111.111.1.11 我不明白为什么即使同一ip出现两次,匹配的ip变量

我正在编写一个简单的服务器套接字接口(如控制台)。我试图实现一个
SEND to xxx.xxx.xxx.xxx
命令,在ip地址
xxx.xxx.xxx
向客户端发送一些信息或命令。所以我试着写这段代码(它在一个类中,这就是为什么出现self参数的原因):

这是控制台:

[Server] >>> SEND TO 111.111.1.11
0
['111.111.1.11', '111.111.1.11']
111.111.1.11
我不明白为什么即使同一ip出现两次,匹配的ip变量仍然保持为0。当我尝试从上一个输出复制ip(使用鼠标和
CTRL+C
)并粘贴(
CTRL-V
)到新行时,使用相同的
SEND to
命令,它工作,并返回:

# where 111.111.1.11 is copied from the previous output (this works even if
# I copy 111.111.1.11 from a previous program run and i paste it for the
# first time in the current run)

[Server] >>> SEND TO 111.111.1.11
2
['111.111.1.11', '111.111.1.11']
111.111.1.11
有什么建议可以解决这个问题吗


非常感谢您抽出时间,请原谅我的英语,我仍在练习

任何
\n
终止您的输入?然后,
ip\u addess
也将包含该
\n
。考虑使用正则表达式组来提取您已经匹配的IP地址。实际上,在输入中没有<代码> \n>代码>,这是一个语句,例如“代码>命令=输入([服务器] > >)<代码> >我认为在IPi地址中,您得到了一个空白。尝试使用ip_地址.strip()。或者您可以执行ip_address=command[8:][.strip(),我已经尝试过了,但它仍然不起作用……如果您正在使用windows,您可能需要删除
\r
任何
\n
终止您的输入?然后,
ip\u addess
也将包含该
\n
。考虑使用正则表达式组来提取您已经匹配的IP地址。实际上,在输入中没有<代码> \n>代码>,这是一个语句,例如“代码>命令=输入([服务器] > >)<代码> >我认为在IPi地址中,您得到了一个空白。尝试使用ip_地址.strip()。或者您可以执行ip_address=command[8:][.strip(),我已经尝试过了,但它仍然不起作用……如果您正在使用windows,您可能还需要删除
\r
# where 111.111.1.11 is copied from the previous output (this works even if
# I copy 111.111.1.11 from a previous program run and i paste it for the
# first time in the current run)

[Server] >>> SEND TO 111.111.1.11
2
['111.111.1.11', '111.111.1.11']
111.111.1.11