Solved

Python 3.4 List manipulation

Posted on 2014-12-01
7
245 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 74

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 74

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now