Solved

Python 3.4 List manipulation

Posted on 2014-12-01
7
250 Views
Last Modified: 2014-12-02
Hi, I have a list which contains  unique and identical sub lists randomly dispersed across the list . There is no limit on the number of identical sub lists which may exist, and they are not necessarily clustered together.

All sub lists have 1 as their first element - which acts as a counter. So, if there are, 3 identical sub lists [1,9,12] - these three sub lists  would be  replaced by a single sub list [3,9,12]

example

MYLIST = [ [1,9,12], [1, 6, 12],  [1, 5, 5],  [1, 8, 32],  [1, 5, 5],  [1,9,12],  [1,9,12] ]  

MYLIST_Converted =  [[3,9,12], [1, 6, 12],  [2, 5, 5], [1, 8, 32]]

Can someone give me a sense of how best to approach this. Been trying for a couple of days without success.

Thanks.
0
Comment
Question by:jameskane
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40474867
Is this school work?
0
 

Author Comment

by:jameskane
ID: 40474943
Its hobby work for  an old retired guy !!   Promise !

I'm trying to convert an old Coldfusion application I did for the local art school.
0
 

Author Comment

by:jameskane
ID: 40474946
kaufmed, see   cannes-beaux-arts.com  ... see right hand side, middle for my name on site for art school
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40475089
This was run in a 2.7.5 environment.
def main():
     MYLIST = [ [1,9,12], [1, 6, 12],  [1, 5, 5],  [1, 8, 32],  [1, 5, 5],  [1,9,12],  [1,9,12] ]
     MYLIST_Converted = []
     MYLIST_Dups = []
     for m in MYLIST:
        if m in MYLIST_Dups:
            continue
        MC= m[:]
        MC[0]=MYLIST.count(m)
        if MC[0]!=1:
            MYLIST_Dups.append(m)
        MYLIST_Converted.append(MC)
     print MYLIST
     print MYLIST_Converted

if __name__ == '__main__':
    main()

Open in new window


Produces the following output:
[[1, 9, 12], [1, 6, 12], [1, 5, 5], [1, 8, 32], [1, 5, 5], [1, 9, 12], [1, 9, 12]]
[[3, 9, 12], [1, 6, 12], [2, 5, 5], [1, 8, 32]]

Open in new window

0
 

Author Closing Comment

by:jameskane
ID: 40475573
INGENIOUS !!!!
works perfectly.
Went through code and learned a lot - thanks for that also.

The only   bit I am struggling to understand are the last two lines

if __name__ == '__main__':
    main()

If you have a minute, could you send me a few words / pointer of explanation ?

Many thanks
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40476329
you don't need to package your code this way.  You can put all of the main function code into your module as is.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40476399
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now