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
Solved

Python 3.4 List manipulation

Posted on 2014-12-01
7
260 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

792 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