将函数的结果存储在列表中 python
Store the result of a function in a list python
简介
我有一个函数可以解析守护程序套接字的输出。我用它来捕捉红外遥控器上按下的键。
def getKey():
while True:
data = sock.recv(128)
data = data.strip()
if (len(data) > 0):
break
words = data.split()
return words[2], words[1]
key = getKey()
print(key)
问题
函数始终 returns 单个字符串对象
输出:
1
<class string>
2
<class string>
7
<class string>
问题
如何将所有这些字符串对象存储到单个列表对象以供进一步使用?
像这样:
[1,2,7]
<class list>
def getKey():
while True:
data = sock.recv(128)
data = data.strip()
if (len(data) > 0):
break
words = data.split()
return words[2], words[1]
keys = []
keys.append(getKey())
keys.append(getKey())
keys.append(getKey())
print(keys)
简介 我有一个函数可以解析守护程序套接字的输出。我用它来捕捉红外遥控器上按下的键。
def getKey():
while True:
data = sock.recv(128)
data = data.strip()
if (len(data) > 0):
break
words = data.split()
return words[2], words[1]
key = getKey()
print(key)
问题 函数始终 returns 单个字符串对象
输出:
1
<class string>
2
<class string>
7
<class string>
问题 如何将所有这些字符串对象存储到单个列表对象以供进一步使用?
像这样:
[1,2,7]
<class list>
def getKey():
while True:
data = sock.recv(128)
data = data.strip()
if (len(data) > 0):
break
words = data.split()
return words[2], words[1]
keys = []
keys.append(getKey())
keys.append(getKey())
keys.append(getKey())
print(keys)