Solved

Python 3.4 List manipulation

Posted on 2014-12-01
7
263 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: 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.

 
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: 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python tuples 2 133
Downgrade Python 3.5 to 3.3 30 1,278
Need help with python script - Insert data from user input into MS SQL 35 150
error with datetime python 1 35
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

749 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