-3

I have following list of lists in python :

[
    u'aaaaa', 
    [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'], 
    u'zzzzzz', 
    [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'], 
    [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], 
    [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'], 
    u'bbbbb', 
    [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'], 
    [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], 
    [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'], 
    [1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going']
]

I want following output in python :

[
    [u'aaaaa', [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time']], 
    [u'zzzzzz', [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time']], 
    [u'bbbbb', [1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going']]
]

Please suggest me how can it possible with manage order in python.

forvas
  • 9,801
  • 7
  • 62
  • 158
  • 4
    If you're talking about removing duplicate values but preserving order, then this should help you: [How do you remove duplicates from a list in Python whilst preserving order?](http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order) – SuperBiasedMan Jul 29 '15 at 08:21
  • Its working but here is something change, i got after follow this link is : u'zzzzzz', [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'], but i want every eleemnt in pair like this [u'zzzzzz', [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time']], [u'bbbbb', [1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going']] please suggest me hows its possible : – ankit dasani Jul 29 '15 at 08:28

3 Answers3

1

The following should give you the desired output. It uses a dictionary to spot duplicate entries.

entries = [
    u'aaaaa', [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'],
    u'zzzzzz', [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'], 
    [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], 
    [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'],
    u'bbbbb', 
    [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'], 
    [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], 
    [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'], 
    [1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going']]

d = {}
output = []
entry = []

for item in entries:
    if type(item) == type([]):
        t = tuple(item)
        if t not in d:
            d[t] = 0
            entry.append(item)
    else:
        if len(entry):
            output.append(entry)

        entry = [item]

output.append(entry)

print output

This gives the following output:

[[u'aaaaa', [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time']], [u'zzzzzz', [1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going'], [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time']], [u'bbbbb', [1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going']]]

Tested using Python 2.7

Update: If a list of lists format is needed, simply add [] to item in the above script as follows::

entry.append([item])

This would give the following output:

[[u'aaaaa', [[1, 6, u'testing', 20.0, 18.0, 2.0, 'In time']]], [u'zzzzzz', [[1, 1, u'xyz ', 30.0, 25.0, 5.0, 'On Going']], [[2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time']]], [u'bbbbb', [[1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going']]]]
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • hello Martin, if as possible please suggest me how its possible : I want inner list of element in list of lists format like this because in print RML report other format is not properly working so please suggest me how i do this :[[u'aaaaa', [ [1, 6, u'testing', 20.0, 18.0, 2.0, 'In time'] ]], [u'Administrator',[ [1, 1, u'xyz', 30.0, 25.0, 5.0, 'On Going'], [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'] ]],[u'bbbbb', [ [1, 7, u'develop', 20.0, 15.0, 5.0, 'On Going'] ]]] – ankit dasani Jul 29 '15 at 11:41
  • You just need to add `[]` to one of the appends. I have updated the answer. – Martin Evans Jul 29 '15 at 15:30
  • I got your answer but sorry, I not need that output .your suggest is proper for single list of element but in that admin have 2 list of elements so i want like this : [u'Administrator', [ [1, 1, u'xyz', 30.0, 25.0, 5.0, 'On Going'], [2, 1, u'abcd', 10.0, 8.0, 2.0, 'In time'], ] ], first and last is properly working after your suggestion but what about this one please suggest me how it possible.Thanks – ankit dasani Jul 30 '15 at 04:28
  • That is exactly what the code produces, I was just displaying it item by item. If you just print `output` you will see. I have made that simple change. – Martin Evans Jul 30 '15 at 07:31
0

If you want all unique values from a list:

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
mylist = [list(x) for x in set(tuple(x) for x in testdata)]
print myset # This is now a set containing all unique values.
# This will not maintain the order of the items
Stiffo
  • 818
  • 6
  • 19
0

1) I really think you should check out Python dictionaries. They would make much more sense, looking at the kind of output you want.

2) In this case, if I understand you correctly, you want to convert a list with elements that are either strings or lists into a list of lists. This list of lists should have a starting element as a string, and the remaining elements as the following list items within the main list, till you hit the next string. (At least that's what it looks like from your example).

output_list = []
for elem in main_list:
    if isinstance(elem,basestring):
        output_list.append([elem])
    else:
        output_list[-1].append(elem)
shashwat
  • 992
  • 1
  • 13
  • 27
  • after trying this code python display error " list index out of range" I want to create pair in that first one is name and second one is one or more than one list in python – ankit dasani Jul 29 '15 at 08:49
  • Change Line 3 to `type(u'')` since you're using only unicode – shashwat Jul 29 '15 at 09:14
  • Or, `isinstance(elem,basestring)` if you'll be using both strings and unicode strings – shashwat Jul 29 '15 at 09:15