0

Background

Updated: Here is the horrendous list I'm working with (please excuse the list's false title). The length and data is subject to change because I'm retrieving this information from an ever-changing database.

Desired Outcome

I will further explain my purposes because I am unsure of the best way to go about this. Each number represents an ID as noted below. I need to create an XML file where Software elements have Target sub-elements based upon the SoftwareID and TargetID matching up in the tuple.

For example:

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 

Attempt

I've played with unpacking the sequence as done here, but the list is much too long for this implementation. I get an error that says Too many values to unpack.
I see now that this is actually a list of tuples instead of just a single tuple, so this wouldn't work anyway.

I've tried this implementation but get an error that says tuple object has no attribute 'split'.

As for the rest of my implementation, I am lost without being able to separate this stupid list.

Question

  • How can I separate the data from this list of tuples in order to use it for the implementation described in the Desired Outcome section?
Community
  • 1
  • 1
Kimbluey
  • 1,199
  • 2
  • 12
  • 23
  • 1
    thats not a tuple ... – Joran Beasley Mar 03 '15 at 17:35
  • 1
    This looks like a list of tuples. Is this correct? – Brobin Mar 03 '15 at 17:36
  • is that just a textfile with a tuple on each line? how is it input into your program – Joran Beasley Mar 03 '15 at 17:36
  • I layed it out like that for readability, but I suppose that was silly - I'll update the link to show what it actually looks like. – Kimbluey Mar 03 '15 at 17:38
  • You get "Too many values to unpack." when you try to do something like `a, y = [1,2,3]`. You need as many variables as you have values in your container element (in my case 3) if you want to use tuple/list unpacking. – Martin Thoma Mar 03 '15 at 17:39
  • 2
    thats still not a tuple (its a list, that has tuples ...) but now we are splitting hairs – Joran Beasley Mar 03 '15 at 17:42
  • @JoranBeasley When I tried using `split()` on it, I get an error saying there is no `split()` function for tuple, which I suppose is why I thought it was a tuple. If it is in fact a list, could you explain why `split()` does not work? – Kimbluey Mar 03 '15 at 17:47
  • split only works on strings .... I suspect you were trying to split an item in the list (the list contains tuples ... each tuple contains 2 elements) – Joran Beasley Mar 03 '15 at 17:50
  • @JoranBeasley Sorry, I should have specified - I used the implementatino found [here](http://stackoverflow.com/questions/6696027/split-elements-of-a-list-in-python), which is when I got that error. – Kimbluey Mar 03 '15 at 17:53
  • OK I have answered below as much as i am willing to for now – Joran Beasley Mar 03 '15 at 17:58
  • @JoranBeasley 10/10 am about to overhaul my question and make it less...incomprehensible? To the best of my abilities anyway -_- – Kimbluey Mar 03 '15 at 17:58

1 Answers1

3

its unclear what the actual problem is ... but maybe this will help resolve the issue

data = {}
for lhs,rhs in my_list_of_tuples:

    try:
        data[lhs].append(rhs)
    except KeyError:
        data[lhs] = [rhs]

print data.items()[:5]

to explain a little more simply lets look at it differently

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]}
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Works great! Thanks a ton. My hope is that this will make creating my XML file much easier. I may edit your answer a little to get rid of the parts where you answered while my question was still confusing, if you don't mind me doing so – Kimbluey Mar 03 '15 at 18:28
  • Eh, I'll just leave it. The rest could be useful to someone else. Thanks for the explanatory comments in the last piece by the way, not many people do that. – Kimbluey Mar 03 '15 at 18:52