在Python2.6中,如何分离和遍历元组列表中的数据? 背景
更新:这里是我的合作伙伴(请原谅列表的假标题)。长度和数据可能会发生变化,因为我正在从不断变化的数据库中检索这些信息 预期结果 我将进一步解释我的目的,因为我不确定最好的方法。每个数字代表一个ID,如下所述。我需要创建一个XML文件,其中软件元素具有基于元组中SoftwareID和TargetID匹配的目标子元素 例如:在Python2.6中,如何分离和遍历元组列表中的数据? 背景,python,tuples,python-2.6,Python,Tuples,Python 2.6,更新:这里是我的合作伙伴(请原谅列表的假标题)。长度和数据可能会发生变化,因为我正在从不断变化的数据库中检索这些信息 预期结果 我将进一步解释我的目的,因为我不确定最好的方法。每个数字代表一个ID,如下所述。我需要创建一个XML文件,其中软件元素具有基于元组中SoftwareID和TargetID匹配的目标子元素 例如: SoftwareID TargetID XML Representation ------------------------------
SoftwareID TargetID XML Representation
----------------------------------------------------------------------
65 115 <-- This Software element (id=65) has only one Target
sub-element (id=115)
138 218 <-- This Software element (id=138) will have multiple
138 219 Target sub-elements (ids=218, 219, 220)
138 220
SoftwareID目标XML表示
----------------------------------------------------------------------
65 115不清楚实际问题是什么。。。但也许这将有助于解决这个问题
data = {}
for lhs,rhs in my_list_of_tuples:
try:
data[lhs].append(rhs)
except KeyError:
data[lhs] = [rhs]
print data.items()[:5]
为了解释得更简单一点,让我们换个角度来看待它
my_list_of_tuples = [(1,2),(3,5),(7,8),(7,9)]
for item in my_list_of_tuples:
#on first loop item=(1,2) ; 2nd loop item=(3,5); 3rd loop item=(7,8)
left_number = item[0]
right_number = item[1]
#these 2 lines can be simplified to
left_number,right_number = item
#now if left number is already in the data dictionary we will append the new right number to its list
try:
data[left_number].append(right_number)
except KeyError: #: if the left_number is not already in our data dictionary
data[left_number] = [right_number] #: create a new list that contains only the right number
#now that we are done we have data that maps left numbers to right numbers
print data
#{1:[2],3:[5],7:[8,9]}
不清楚实际问题是什么。。。但也许这将有助于解决这个问题
data = {}
for lhs,rhs in my_list_of_tuples:
try:
data[lhs].append(rhs)
except KeyError:
data[lhs] = [rhs]
print data.items()[:5]
为了解释得更简单一点,让我们换个角度来看待它
my_list_of_tuples = [(1,2),(3,5),(7,8),(7,9)]
for item in my_list_of_tuples:
#on first loop item=(1,2) ; 2nd loop item=(3,5); 3rd loop item=(7,8)
left_number = item[0]
right_number = item[1]
#these 2 lines can be simplified to
left_number,right_number = item
#now if left number is already in the data dictionary we will append the new right number to its list
try:
data[left_number].append(right_number)
except KeyError: #: if the left_number is not already in our data dictionary
data[left_number] = [right_number] #: create a new list that contains only the right number
#now that we are done we have data that maps left numbers to right numbers
print data
#{1:[2],3:[5],7:[8,9]}
不清楚实际问题是什么。。。但也许这将有助于解决这个问题
data = {}
for lhs,rhs in my_list_of_tuples:
try:
data[lhs].append(rhs)
except KeyError:
data[lhs] = [rhs]
print data.items()[:5]
为了解释得更简单一点,让我们换个角度来看待它
my_list_of_tuples = [(1,2),(3,5),(7,8),(7,9)]
for item in my_list_of_tuples:
#on first loop item=(1,2) ; 2nd loop item=(3,5); 3rd loop item=(7,8)
left_number = item[0]
right_number = item[1]
#these 2 lines can be simplified to
left_number,right_number = item
#now if left number is already in the data dictionary we will append the new right number to its list
try:
data[left_number].append(right_number)
except KeyError: #: if the left_number is not already in our data dictionary
data[left_number] = [right_number] #: create a new list that contains only the right number
#now that we are done we have data that maps left numbers to right numbers
print data
#{1:[2],3:[5],7:[8,9]}
不清楚实际问题是什么。。。但也许这将有助于解决这个问题
data = {}
for lhs,rhs in my_list_of_tuples:
try:
data[lhs].append(rhs)
except KeyError:
data[lhs] = [rhs]
print data.items()[:5]
为了解释得更简单一点,让我们换个角度来看待它
my_list_of_tuples = [(1,2),(3,5),(7,8),(7,9)]
for item in my_list_of_tuples:
#on first loop item=(1,2) ; 2nd loop item=(3,5); 3rd loop item=(7,8)
left_number = item[0]
right_number = item[1]
#these 2 lines can be simplified to
left_number,right_number = item
#now if left number is already in the data dictionary we will append the new right number to its list
try:
data[left_number].append(right_number)
except KeyError: #: if the left_number is not already in our data dictionary
data[left_number] = [right_number] #: create a new list that contains only the right number
#now that we are done we have data that maps left numbers to right numbers
print data
#{1:[2],3:[5],7:[8,9]}
这不是一个元组…这看起来像一个元组列表。这是正确的吗?这只是一个文本文件,每行有一个元组吗?它是如何输入到你的程序中的?为了可读性,我把它像那样放了出来,但我认为这很愚蠢-我会更新链接以显示它的实际外观。这仍然不是一个元组(它是一个列表,有元组…),但现在我们正在拆分头发,而不是元组…这看起来像是一个元组列表。这是正确的吗?这只是一个文本文件,每行有一个元组吗?它是如何输入到你的程序中的?为了可读性,我把它像那样放了出来,但我认为这很愚蠢-我会更新链接以显示它的实际外观。这仍然不是一个元组(它是一个列表,有元组…),但现在我们正在拆分头发,而不是元组…这看起来像是一个元组列表。这是正确的吗?这只是一个文本文件,每行有一个元组吗?它是如何输入到你的程序中的?为了可读性,我把它像那样放了出来,但我认为这很愚蠢-我会更新链接以显示它的实际外观。这仍然不是一个元组(它是一个列表,有元组…),但现在我们正在拆分头发,而不是元组…这看起来像是一个元组列表。这是正确的吗?这只是一个文本文件,每行有一个元组吗?它是如何输入到你的程序中的?为了可读性,我把它像那样放了出来,但我认为这很愚蠢-我会更新链接以显示它的实际外观。这仍然不是一个元组(它是一个列表,有元组…),但现在我们正在拆分hairsWorks很棒!非常感谢。我希望这将使创建XML文件变得更加容易。我可能会对你的答案稍加修改,以消除你在我的问题仍然令人困惑时回答的部分,如果你不介意我这样做,我就不提了。其余的可能对其他人有用。谢谢你在最后一篇文章中的解释性评论,顺便说一句,没有很多人这么做。效果很好!非常感谢。我希望这将使创建XML文件变得更加容易。我可能会对你的答案稍加修改,以消除你在我的问题仍然令人困惑时回答的部分,如果你不介意我这样做,我就不提了。其余的可能对其他人有用。谢谢你在最后一篇文章中的解释性评论,顺便说一句,没有很多人这么做。效果很好!非常感谢。我希望这将使创建XML文件变得更加容易。我可能会对你的答案稍加修改,以消除你在我的问题仍然令人困惑时回答的部分,如果你不介意我这样做,我就不提了。其余的可能对其他人有用。谢谢你在最后一篇文章中的解释性评论,顺便说一句,没有很多人这么做。效果很好!非常感谢。我希望这将使创建XML文件变得更加容易。我可能会对你的答案稍加修改,以消除你在我的问题仍然令人困惑时回答的部分,如果你不介意我这样做,我就不提了。其余的可能对其他人有用。谢谢你在最后一篇文章中的解释性评论,顺便说一句,没有多少人这么做。