Python Forum
list of list to list of tuple?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of list to list of tuple?
#1
network= [['0 1'], ['0 2'], ['0 3'], ['1 4'], ['1 6'], ['1 7'], ['1 9'], ['2 3'], ['2 6'], ['2 8'], ['2 9']]

I have a list of lists (above) and I am trying to create a list of tuples where it looks like this:

[(0, [1, 2, 3]), (1, [0, 4, 6, 7, 9]), (2, [0, 3, 6, 8, 9]),]

So far I have:
network= [['0 1'], ['0 2'], ['0 3'], ['1 4'], ['1 6'], ['1 7'], ['1 9'], ['2 3'], ['2 6'], ['2 8'], ['2 9']]
friends=[]

for i in range(len(network)):
    element= (network[i][0][0], i+1)
    friends.append(element)
print(friends)


And it outputs:
[('0', 1), ('0', 2), ('0', 3), ('1', 4), ('1', 5), ('1', 6), ('1', 7), ('2', 8), ('2', 9), ('2', 10), ('2', 11)]

I am unsure how to take the next step,plus this output is wrong, how do I convert this into what is expected

Please help
Reply
#2
How do I convert this list of lists:

[['0', '1'], ['0', '2'], ['0', '3'], ['1', '4'], ['1', '6'], ['1', '7'], ['1', '9'], ['2', '3'], ['2', '6'], ['2', '8'], ['2', '9']]

To this list of tuples:

[(0, [1, 2, 3]), (1, [0, 4, 6, 7, 9]), (2, [0, 3, 6, 8, 9])]

I am unsure how to implement this next step? (I can't use dictionaries,
sets, deque, bisect module. You can though, and in fact should, use .sort or sorted functions.)

Here is my attempt:

    network= [['10'], ['0 1'], ['0 2'], ['0 3'], ['1 4'], ['1 6'], ['1 7'], ['1 9'], ['2 3'], ['2 6'], ['2 8'], ['2 9']]
    network.remove(network[0])
    friends=[]
    
    for i in range(len(network)):
        element= (network[i][0]).split(' ')
        friends.append(element)
    
    
    t=len(friends)
    lst=[]
    
    for i in range(t):
        a= (friends[i][0])
        if a not in lst:
            lst.append(int(a))
            for i in range(t):
                if a == friends[i][0]:
                    b=(friends[i][1])
                    lst.append([b])
    print(tuple(lst))
It outputs:

  (0, ['1'], ['2'], ['3'], 0, ['1'], ['2'], ['3'], 0, ['1'], ['2'], ['3'], 1, ['4'], ['6'], ['7'], ['9'], 1, ['4'], ['6'], ['7'], ['9'], 1, ['4'], ['6'], ['7'], ['9'], 1, ['4'], ['6'], ['7'], ['9'], 2, ['3'], ['6'], ['8'], ['9'], 2, ['3'], ['6'], ['8'], ['9'], 2, ['3'], ['6'], ['8'], ['9'], 2, ['3'], ['6'], ['8'], ['9'])
I am very close it seems, but not sure what to do??
Reply
#3
sort the list by the first element, then loop over it and check what the first element in each sub-list is. if it is different from the [first] one in the previous sub-list  - add new element in the result list and start populating it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A function that checks if the list is sorted pooyan89 16 16,606 Apr-22-2024, 09:14 AM
Last Post: DeaD_EyE
Sad IMC Calculator but with a list of patients Booba 1 983 Jun-11-2023, 07:21 AM
Last Post: deanhystad
  List joining deatz 14 3,247 Dec-21-2022, 03:34 AM
Last Post: deatz
  Help with list homework eyal123 5 1,645 Nov-18-2022, 03:46 PM
Last Post: deanhystad
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,510 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  help with adding duplicates elements together in a list 2ECC3O 5 2,047 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,303 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
  list digit into number Voldyy 2 1,546 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  How to remove extra space from output list? longmen 3 1,793 May-05-2022, 11:04 PM
Last Post: longmen
  How to fix list index out of range longmen 26 5,967 Apr-27-2022, 05:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020