i'm lost in list manipulation

G

GrelEns

hello,

having a list like ['a', 'b', 'c', 'd'] i would like to get

[['a', '-', 'b', 'c', 'd'],
['a', 'b', '-', 'c', 'd'],
['a', 'b', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c', 'd'],
['a', 'b', '-', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c','-', 'd']]

that is adding successively an item between at all the available place, and
i just can't get my brain doing that :(

thank a lot if you could help...
 
S

Scott David Daniels

GrelEns said:
hello,

having a list like ['a', 'b', 'c', 'd'] i would like to get

[['a', '-', 'b', 'c', 'd'],
['a', 'b', '-', 'c', 'd'],
['a', 'b', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c', 'd'],
['a', 'b', '-', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c','-', 'd']]

that is adding successively an item between at all the available place, and
i just can't get my brain doing that :(
Maybe:
Think of all possible places. How many are there? At each of these
places you either put a dash or not. Sounds like binary to me.
 
W

wes weston

GrelEns said:
hello,

having a list like ['a', 'b', 'c', 'd'] i would like to get

[['a', '-', 'b', 'c', 'd'],
['a', 'b', '-', 'c', 'd'],
['a', 'b', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c', 'd'],
['a', 'b', '-', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c','-', 'd']]

that is adding successively an item between at all the available place, and
i just can't get my brain doing that :(

thank a lot if you could help...


GrelEns,
Scott has it. For a start string of 4 chars there are 3 gaps/spaces
for the dash/not dash. So, there are 8 possible results. One of these
has no dashes - you may want to throw this one out. The "gap" strings
needs to be:
"000"
"001"
"010"
"011"
"100"
"101"
"110"
"111"
where the 1's are your character; "-"
Throw out the first one and insert into the gaps in the original string.
wes
 
P

Paul Watson

GrelEns said:
hello,

having a list like ['a', 'b', 'c', 'd'] i would like to get

[['a', '-', 'b', 'c', 'd'],
['a', 'b', '-', 'c', 'd'],
['a', 'b', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c', 'd'],
['a', 'b', '-', 'c', '-', 'd'],
['a', '-', 'b', '-', 'c','-', 'd']]

that is adding successively an item between at all the available place, and
i just can't get my brain doing that :(

thank a lot if you could help...

Does this look like it would work?

alist = ['a', 'b', 'c', 'd']
nlist = range(len(alist) - 1)
finallist = []

for x in range(2 ** (len(alist) - 1)):
if x == 0: continue #remove this if you want one with no dashes
rlist = [alist[0]]
for y in nlist:
if x & (1 << y): rlist.append('-')
rlist.append(alist[y + 1])
finallist.append(rlist)
del rlist

print finallist


$ ./listins.py
[['a', '-', 'b', 'c', 'd'], ['a', 'b', '-', 'c', 'd'], ['a', '-', 'b', '-',
'c', 'd'], ['a', 'b', 'c', '-', 'd'], ['a',
'-', 'b', 'c', '-', 'd'], ['a', 'b', '-', 'c', '-', 'd'], ['a', '-', 'b',
'-', 'c', '-', 'd']]
 
G

Greg Ewing (using news.cis.dfn.de)

Here's an alternative solution using recursion:

def di(prefix, suffix, result):
head = suffix[0]
tail = suffix[1:]
prefix2 = prefix + [head]
if not tail:
result.append(prefix2)
else:
di(prefix2, tail, result)
di(prefix2 + ['-'], tail, result)

def insert_dashes(items):
result = []
di([], items, result)
return result

print insert_dashes(['a'])
print insert_dashes(['a', 'b'])
print insert_dashes(['a', 'b', 'c', 'd'])

cosc353% python insdash.py
[['a']]
[['a', 'b'], ['a', '-', 'b']]
[['a', 'b', 'c', 'd'], ['a', 'b', 'c', '-', 'd'], ['a', 'b', '-', 'c', 'd'],
['a', 'b', '-', 'c', '-', 'd'], ['a', '-', 'b', 'c', 'd'], ['a', '-', 'b', 'c',
'-', 'd'], ['a', '-', 'b', '-', 'c', 'd'], ['a', '-', 'b', '-', 'c', '-', 'd']]
 
G

GrelEns

btw please give your opinion on this one :

def build(l, n):
result = []
if n == 1:
for e in l:
result.append([e])
else:
subSet = build(l, n - 1)
for n_uples in subSet:
iLast = l.index(n_uples[-1])
if iLast < len(l) - 1:
for e in l[iLast + 1:]:
tmp = n_uples + [e]
result.append(tmp)
return result

def allSets(l):
"""from [1,2,3] will build [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2,
3]]"""
result = []
for i in range(1, len(l)):
result += build(l, i)
result.append(l)
return result

def do_it(l):
r = range(1,len(l))
result = []
for x in allSets(r):
tmp = []
for i in range(len(l)):
if i in x:
tmp.append('-')
tmp.append(l)
result.append(tmp)
return result
[['a', '-', 'b', 'c', 'd'], ['a', 'b', '-', 'c', 'd'], ['a', 'b', 'c', '-',
'd'], ['a', '-', 'b', '-', 'c', 'd'], ['a', '-', 'b', 'c', '-', 'd'], ['a',
'b', '-', 'c', '-', 'd'], ['a', '-', 'b', '-', 'c', '-', 'd']]

best
 
G

GrelEns

GrelEns said:
btw please give your opinion on this one :

def build(l, n):
result = []
if n == 1:
for e in l:
result.append([e])
else:
subSet = build(l, n - 1)
for n_uples in subSet:
iLast = l.index(n_uples[-1])
if iLast < len(l) - 1:
for e in l[iLast + 1:]:
tmp = n_uples + [e]
result.append(tmp)
return result

def allSets(l):
"""from [1,2,3] will build [[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2,
3]]"""
result = []
for i in range(1, len(l)):
result += build(l, i)
result.append(l)
return result

i'm not happy with this code, must be an other shorter alternative, have you
any siggestions on how to build these sets [[1], [2], [3], [1, 2], [1, 3],
[2, 3], [1, 2, 3]] from [1,2,3] ?

best
 
P

Paul Rubin

GrelEns said:
i'm not happy with this code, must be an other shorter alternative, have you
any siggestions on how to build these sets [[1], [2], [3], [1, 2], [1, 3],
[2, 3], [1, 2, 3]] from [1,2,3] ?

def subsets(lst):
def subgen(lst):
if not lst:
yield lst
return
for s in subsets(lst[1:]):
yield s
yield [lst[0]] + s
return list(subgen([1,2,3]))[1:] # discard empty subset

print subsets([1,2,3])
 
G

GrelEns

Paul Rubin said:
GrelEns said:
i'm not happy with this code, must be an other shorter alternative, have you
any siggestions on how to build these sets [[1], [2], [3], [1, 2], [1, 3],
[2, 3], [1, 2, 3]] from [1,2,3] ?

def subsets(lst):
def subgen(lst):
if not lst:
yield lst
return
for s in subsets(lst[1:]):
yield s
yield [lst[0]] + s
return list(subgen([1,2,3]))[1:] # discard empty subset

print subsets([1,2,3])

sorry, i miss something here probably loosing indentation as my version
contain an infnite recursion,

can you repost it ? thanx

btw it looks like very impressive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top