473,796 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i'm lost in list manipulation

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...
Jul 18 '05 #1
3 1169
GrelEns wrote:
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.

--
-Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #2


GrelEns wrote:
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

Jul 18 '05 #3

"GrelEns" <gr*****@NOSPAM yahoo.NOTNEEDED fr> wrote in message
news:40******** *************** @news.free.fr.. .
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(al ist[y + 1])
finallist.appen d(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']]
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1803
by: Farhad | last post by:
Hi, in the following example there is a output difference in version 5.00 and 5.8 of Perl. In case of a missmatch in element 2, the whole element gets lost instead of an "undef" value. Does anybody know why in version 5.8 we lost the element and is that an error in Perl interpreter or does it work as designed. Thanks Farhad
1
8295
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two dropdown lists. If you change the selection of the left one (e.g. choose parentoption2), it should open up page2.htm in a popup window.
6
4603
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any potential pitfalls I'd be extremely grateful. Cheers Steve
77
17066
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it, sets have it, why do lists have to be second class citizens?
6
3864
by: Konstantin Andreev | last post by:
Hello, all. Let the tablespace SY810T4K is almost full: --------------- db2 =list tablespaces show detail ... Name = SY810T4K Type = Database managed space Contents = Any data Total pages = 332800
25
2755
by: PNY | last post by:
Hi there, I am having some trouble with list manipulation and was hoping someone could help me. I have no problem reading in a text file as a list using filename.readlines(). However, I am having some trouble with searching through this list to display the elements that I want. I have a text file with this general format: *************** text text text text
0
2353
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market Manipulation Profiling Tools Set is a set of
2
6290
by: dave.dex | last post by:
Hi all, I've been searching the docs like mad and I'm a little new to python so apologies if this is a basic question. I would like to extract the results of the following query into a list - SELECT columnname FROM tablename. I use the following code. # Create a connection object and create a cursor db = MySQLdb.Connect(<my-db-info)
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.