How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]?
Thanks! 8 1207
try to describe what you want as it is not very obvious, and has syntax
error.
Shi Mu wrote: How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
try to describe what you want as it is not very obvious, and has syntax
error.
Shi Mu wrote: How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
Shi Mu wrote: How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
From what I gather try:
a = [1,2,4]
n = list()
for i in a:
index = a.index(i) + 1
for x in a[index:]:
n.append([i, x])
print n
more elegant ways to do this, but its a start.
hope this helps.
Adonis
Shi Mu wrote: How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
From what I gather try:
a = [1,2,4]
n = list()
for i in a:
index = a.index(i) + 1
for x in a[index:]:
n.append([i, x])
print n
more elegant ways to do this, but its a start.
hope this helps.
Adonis
Shi Mu wrote: How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
You want [[1,2],[1,4],[2,4]]? That is, all combinations of 2 items
from
the list? You might want to look at: http://aspn.activestate.com/ASPN/Coo.../Recipe/190465 import * from xpermutations [x for x in UniqueCombinations ([1,2,4], 2)]
[[1, 2], [1, 4], [2, 4]]
That web page also gives urls to other recipies that do the same thing.
Shi Mu wrote: How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
You want [[1,2],[1,4],[2,4]]? That is, all combinations of 2 items
from
the list? You might want to look at: http://aspn.activestate.com/ASPN/Coo.../Recipe/190465 import * from xpermutations [x for x in UniqueCombinations ([1,2,4], 2)]
[[1, 2], [1, 4], [2, 4]]
That web page also gives urls to other recipies that do the same thing. ru***@yahoo.com wrote: Shi Mu wrote:
How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
You want [[1,2],[1,4],[2,4]]? That is, all combinations of 2 items from the list? You might want to look at: http://aspn.activestate.com/ASPN/Coo.../Recipe/190465
import * from xpermutations [x for x in UniqueCombinations ([1,2,4], 2)]
[[1, 2], [1, 4], [2, 4]]
That web page also gives urls to other recipies that do the same thing.
Dang, that's better than my version.
$ ./combinations.py 2 1 2 4
[['1', '2'], ['1', '4'], ['2', '4']]
def combinations( universe, n=None ):
"""
Return all possible combinations of length "n," where the
elements of each combination are taken from the list "universe."
"""
result = []
if n == None:
n = len( universe )
if n > len( universe ):
# No combination of elements can have cardinality
# greater than universe, which is by definition the list
# of all possible elements.
pass
elif n < 0:
# No combination can have negative cardinaltiy.
pass
elif n == 0:
# Only the empty combination has cardinality 0.
result.append( [ ] )
else: # 0 < n <= len( universe )
for i in xrange( len( universe ) ):
elem = universe[i]
post = universe[i+1:]
for c in combinations( post, n - 1 ):
choice = [ elem ]
choice.extend( c )
result.append( choice )
return result
if __name__ == "__main__":
import sys
if len( sys.argv ) < 2:
sys.stderr.write(
"usage: %s <n> [elements ...]\n" % sys.argv[0] )
sys.exit(1)
n = int( sys.argv[1] )
print repr( combinations( sys.argv[2:], n ) ) ru***@yahoo.com wrote: Shi Mu wrote:
How to run a function to make [1,2,4] become [[1,2],1,4],[2,4]]? Thanks!
You want [[1,2],[1,4],[2,4]]? That is, all combinations of 2 items from the list? You might want to look at: http://aspn.activestate.com/ASPN/Coo.../Recipe/190465
import * from xpermutations [x for x in UniqueCombinations ([1,2,4], 2)]
[[1, 2], [1, 4], [2, 4]]
That web page also gives urls to other recipies that do the same thing.
Dang, that's better than my version.
$ ./combinations.py 2 1 2 4
[['1', '2'], ['1', '4'], ['2', '4']]
def combinations( universe, n=None ):
"""
Return all possible combinations of length "n," where the
elements of each combination are taken from the list "universe."
"""
result = []
if n == None:
n = len( universe )
if n > len( universe ):
# No combination of elements can have cardinality
# greater than universe, which is by definition the list
# of all possible elements.
pass
elif n < 0:
# No combination can have negative cardinaltiy.
pass
elif n == 0:
# Only the empty combination has cardinality 0.
result.append( [ ] )
else: # 0 < n <= len( universe )
for i in xrange( len( universe ) ):
elem = universe[i]
post = universe[i+1:]
for c in combinations( post, n - 1 ):
choice = [ elem ]
choice.extend( c )
result.append( choice )
return result
if __name__ == "__main__":
import sys
if len( sys.argv ) < 2:
sys.stderr.write(
"usage: %s <n> [elements ...]\n" % sys.argv[0] )
sys.exit(1)
n = int( sys.argv[1] )
print repr( combinations( sys.argv[2:], n ) ) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marco Aschwanden |
last post by:
Hi
I don't have to talk about the beauty of Python and its clear and
readable syntax... but there are a few things that striked me while
learning Python.
I have collected those thoughts. I am...
|
by: fdsl ysnh |
last post by:
--- python-list-request@python.orgдµÀ:
> Send Python-list mailing list submissions to
> python-list@python.org
>
> To subscribe or unsubscribe via the World Wide Web,
> visit
>...
|
by: kj |
last post by:
How can one test if a pointer has been "freed" (i.e. with free())?
My naive assumption was that such a pointer would equal NULL, but
not so.
Thanks,
kj
--
NOTE: In my address everything...
|
by: Christoph Zwerschke |
last post by:
Sometimes I find myself stumbling over Python issues which have to do
with what I perceive as a lack of orthogonality.
For instance, I just wanted to use the index() method on a tuple which
does...
|
by: pinkfog |
last post by:
Hello,all
I m just a newbie to c,and now i m learning quick sorting,i dont very
undertand this algorrithm,can someone kind to explain it to me?
the code:
<quote>
void quick_sort(int a ,int low...
|
by: neilcancer |
last post by:
My English is poor...
There are some sort algorithms which sort a sequencial list. The
sequencial list was defined in "list-seq.h".
sort_and_time() accepts one of these sort functions. It uses...
|
by: chrisben |
last post by:
Hi,
Here is the scenario. I have a list of IDs and there are multiple threads
trying to add/remove/read from this list. I can do in C#
1. create Hashtable hList = Hashtable.Synchronized(new...
|
by: Steven T. Hatton |
last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash,
Mathematica, SML, or LISP. A college teacher recently posted to this
newsgroup regarding her observation that there has...
|
by: Ruan |
last post by:
My confusion comes from the following piece of code:
memo = {1:1, 2:1}
def fib_memo(n):
global memo
if not n in memo:
memo = fib_memo(n-1) + fib_memo(n-2)
return memo
I used to think that...
|
by: BillG |
last post by:
I am developing a business app using asp.net that will have 2 different
types of forms, a list view of data and then a detail view. The list views
are placed on a content panel in a master page. ...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |