python 数据存放

hukui161 2011-11-07 10:31:13

1.xr = [['a',1], ['a', 2], ['b', 3], ['b',4],['c',5]]
2.xr = [['a',1], ['b', 2], ['c', 3], ['d',4],['e',5]]
3.xr = [['a',1], ['a',2], ['b', 3], ['c',4], ['c',5']]
4.xr = [['a', 1]]


将xr中的数据存放到两个[]中
xr 在为1时存放为 res1 = [1,3, 5], res2= [2,4]
xr 在为2时存放为 res1 = [1,2,3,4,5] res2 = []
xr 在为3时存放为 res1 = [1, 3, 4] res2 = [2, 5]
xr 在为4时存放为 res1 = [1] res2 = []

这个用python怎么实现了?
...全文
138 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hukui161 2011-11-08
  • 打赏
  • 举报
回复
好像是可以。
WaistCoat07 2011-11-08
  • 打赏
  • 举报
回复
你可以试下我的代码,完全符合你的要求。
hukui161 2011-11-08
  • 打赏
  • 举报
回复
你可能没看懂意思。就是XR的数据有几种类型,当xr中有[['a',1], ['a', 2]]的时候,当有两个'a'出现的时候 将第一个出现的 ['a',1] 中的1加入res中,将['a', 2] 中的2加入到res2中。
当只有一个['c',5]时, 将5加入到 res1中。
WaistCoat07 2011-11-08
  • 打赏
  • 举报
回复
def func(data):
temp = []
res1 = []
res2 = []
for item in data:
if item[0] in temp:
res2.append(item[1])
else:
res1.append(item[1])
temp.append(item[0])
return (res1, res2)

def main():
xr = [['a',1], ['a', 2], ['b', 3], ['b',4],['c',5]]
print(func(xr))
hukui161 2011-11-08
  • 打赏
  • 举报
回复

def GetNum():
global g_InactiveNumber
global g_ActiveNumber
g_InactiveNumber = []
if(xr[0] == xr[-1]):
g_ActiveNumber.append(xr[0][1])
return
lastNum = 0
lastModuleID = 0
for tmpList in xr:
if(lastModuleID == 0):
lastModuleID = tmpList[0]
lastNum = tmpList[1]
elif(lastModuleID == tmpList[0]):
if(lastSlotNum not in g_ActiveNumber):
g_ActiveNumber.append(lastNum)
if(tmpList[1] not in g_InactiveNumber):
g_InactiveNumber.append(tmpList[1])

elif(lastModuleID != tmpList[0]):
if(lastSlotNum not in g_InactiveNumber or lastNum not in g_ActiveNumber):
g_ActiveNumber.append(lastSlotNum)
g_ActiveNumber.append(tmpList[1])
else:
g_ActiveNumber.append(tmpList[1])
lastModuleID = tmpList[0]
lastNum = tmpList[1]

hukui161 2011-11-08
  • 打赏
  • 举报
回复
这样好像不行了。XR 存放的数据可能有上面四种情况。
WaistCoat07 2011-11-08
  • 打赏
  • 举报
回复
xr = [['a',1], ['a', 2], ['b', 3], ['b',4],['c',5]]
# xr=[['a',1], ['b', 2], ['c', 3], ['d',4],['e',5]]
# xr=[['a',1], ['a',2], ['b', 3], ['c',4], ['c',5']]
# xr=[['a', 1]]
res1 = []
res2 = []
temp = []

for item in xr:
if item[0] in temp:
res2.append(item[1])
else:
res1.append(item[1])
temp.append(item[0])
WaistCoat07 2011-11-08
  • 打赏
  • 举报
回复
没看懂问题
hukui161 2011-11-07
  • 打赏
  • 举报
回复
没人回啊?

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧