Solved

Python 3.4 List manipulation

Posted on 2014-12-01
7
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

734 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