473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Building unique comma-delimited list?

I've got a silly little problem that I'm solving in C++, but I got to
thinking about how much easier it would be in Python. Here's the
problem:

You've got a list of words (actually, they're found by searching a
data structure on the fly, but for now let's assume you've got them as
a list). You need to create a comma-delimited list of these words.
There might be duplicates in the original list, which you want to
eliminate in the final list. You don't care what order they're in,
except that there is a distinguised word which must come first if it
appears at all.

Some examples ("foo" is the distinguised word):

["foo"] => "foo"
["foo", "bar"] => "foo, bar"
["bar", "foo"] => "foo, bar"
["bar", "foo", "foo", "baz", "bar"] => "foo, bar, baz" or "foo, baz, bar"

The best I've come up with is the following. Can anybody think of a
simplier way?

--------------------
words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]

# Eliminate the duplicates; probably use set() in Python 2.4
d = dict()
for w in words:
d[w] = w

if d.has_key ("foo"):
newWords = ["foo"]
del (d["foo"])
else:
newWords = []

for w in d.keys():
newWords.append (w)

s = ', '.join (newWords)
print s
--------------------
Jul 18 '05 #1
5 2019
ro*@panix.com (Roy Smith) writes:
I've got a silly little problem that I'm solving in C++, but I got to
thinking about how much easier it would be in Python. Here's the
problem:

You've got a list of words (actually, they're found by searching a
data structure on the fly, but for now let's assume you've got them as
a list). You need to create a comma-delimited list of these words.
There might be duplicates in the original list, which you want to
eliminate in the final list. You don't care what order they're in,
except that there is a distinguised word which must come first if it
appears at all.

Some examples ("foo" is the distinguised word):

["foo"] => "foo"
["foo", "bar"] => "foo, bar"
["bar", "foo"] => "foo, bar"
["bar", "foo", "foo", "baz", "bar"] => "foo, bar, baz" or "foo, baz, bar"

The best I've come up with is the following. Can anybody think of a
simplier way?

....

How about:

..>>> words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]
..>>> ', '.join(dict( ( (w,w) for w in words ) ).keys())
'baz, foo, bar'
..>>> words = ["foo",]
..>>> ', '.join(dict( ( (w,w) for w in words ) ).keys())
'foo'

or with Python 2.3 or higher:

..>>> import sets
..>>> words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]
..>>> ', '.join(sets.Set(words))
'baz, foo, bar'
..>>> words = ["foo",]
..>>> ', '.join(sets.Set(words))
'foo'
Kind regards
Berthold
--
be******@xn--hllmanns-n4a.de / <http://höllmanns.de/>
bh***@web.de / <http://starship.python.net/crew/bhoel/>
Jul 18 '05 #2
Roy Smith wrote:
You've got a list of words (actually, they're found by searching a
data structure on the fly, but for now let's assume you've got them as
a list). You need to create a comma-delimited list of these words.
There might be duplicates in the original list, which you want to
eliminate in the final list. You don't care what order they're in,
except that there is a distinguised word which must come first if it
appears at all.

Some examples ("foo" is the distinguised word):

["foo"] => "foo"
["foo", "bar"] => "foo, bar"
["bar", "foo"] => "foo, bar"
["bar", "foo", "foo", "baz", "bar"] => "foo, bar, baz" or "foo, baz, bar"

The best I've come up with is the following. Can anybody think of a
simplier way?

Who knows whether this is "simpler", but it does demonstrate that you
can customize the sort of a list:

#!/usr/bin/env python

def makesorter(first):
"""Return a sort function that sorts first to the top."""
def sorter(x, y):
if x == first:
return -1
elif y == first:
return 1
else:
return 0
return sorter

words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]
first = 'foo'
sorter = makesorter(first)
unique = {}
for word in words:
unique[word] = word
keys = unique.keys()
keys.sort(sorter)
print ', '.join(keys)
Jul 18 '05 #3
How about (for 2.4 or 2.3 using "from collections import Set as set":

def combine(source, special='foo'):
parts = set(source)
if special in parts:
return ', '.join([special] + list(parts - set([special])))
return ', '.join(parts)

--Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #4
Roy Smith wrote:
The best I've come up with is the following. Can anybody think of a
simplier way?

--------------------
words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]

# Eliminate the duplicates; probably use set() in Python 2.4
d = dict()
for w in words:
d[w] = w

if d.has_key ("foo"):
newWords = ["foo"]
del (d["foo"])
else:
newWords = []

for w in d.keys():
newWords.append (w)

s = ', '.join (newWords)
print s
--------------------


You need to make the dictionary and list types work harder for you. They
have a variety of methods you might find useful.
words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]
distinguished = ["foo"]
d = dict.fromkeys(words, True)
newwords = [ w for w in distinguished if d.pop(w, False) ]
newwords.extend(d.keys())
newwords ['foo', 'baz', 'bar']

Jul 18 '05 #5
On 5 Jan 2005 17:05:40 -0500, ro*@panix.com (Roy Smith) wrote:
I've got a silly little problem that I'm solving in C++, but I got to
thinking about how much easier it would be in Python. Here's the
problem:

You've got a list of words (actually, they're found by searching a
data structure on the fly, but for now let's assume you've got them as
a list). You need to create a comma-delimited list of these words.
There might be duplicates in the original list, which you want to
eliminate in the final list. You don't care what order they're in,
except that there is a distinguised word which must come first if it
appears at all.

Some examples ("foo" is the distinguised word):

["foo"] => "foo"
["foo", "bar"] => "foo, bar"
["bar", "foo"] => "foo, bar"
["bar", "foo", "foo", "baz", "bar"] => "foo, bar, baz" or "foo, baz, bar"

The best I've come up with is the following. Can anybody think of a
simplier way?
(Not tested beyond what you see ;-)
python 2.4:
words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]
w2 = list(t[1] for t in sorted((w!='foo', w) for w in set(words)))
w2 ['foo', 'bar', 'baz']

Gets you a sort in the bargain ;-)
--------------------
words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]

# Eliminate the duplicates; probably use set() in Python 2.4 Yup, but 2.3 can be a one-liner too:
words = ["foo", "bar", "baz", "foo", "bar", "foo", "baz"]
w2 = ('foo' in words and ['foo'] or []) + [w for w in dict(zip(words,words)) if w!='foo']
w2

['foo', 'baz', 'bar']

Not sorted, but foo is out front.
d = dict()
for w in words:
d[w] = w

if d.has_key ("foo"):
newWords = ["foo"]
del (d["foo"])
else:
newWords = []

for w in d.keys():
newWords.append (w)

s = ', '.join (newWords)
print s
--------------------


Regards,
Bengt Richter
Jul 18 '05 #6

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

Similar topics

2
by: Bennett Haselton | last post by:
I'm looking for a PHP tutorial that specializes in how to build sites that are based around user logins. i.e. the user logs in on the front page, and are taken to a main login page where fields on...
27
by: Alberto Vera | last post by:
Hello: I have the next structure: How Can I make it using Python? How Can I update the value of 6?
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
2
by: Raterus | last post by:
Hi, I'm looking for ideas for the most efficient way to accomplish this. I have a string representing names a person goes by. "John Myers Joe John Myers" And I need to parse it in such a...
11
by: frizzle | last post by:
Hi groupies I'm building a news site, to wich a user can add new items into a mySQL db. It's still in testfase, but it's so extremely slow, i want to figure out what i'm doing wrong, or what to...
2
by: ILCSP | last post by:
Hi, I have a sql table containing the answers for some tests. The information in this table is presented vertically and I need to create strings with them. I know how to read the data in VB.Net...
2
by: Steve Richter | last post by:
KeyValuePair<string,stringhas a ToString method that returns the KeyValue pair seperated by a comma and enclosed in : Is this method used as a building block for serialization? The reason I...
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
1
by: Twanne | last post by:
I've got this query Set rst4 = db.OpenRecordset("SELECT L,M,S FROM referenties WHERE type = '" & Form_PatChoise.ctype & "' AND geslacht = '" & Form_PatChoise.Sex & "' AND leeftijd = '" &...
25
by: yaaara | last post by:
Hello, I hope someone can help me in resolving the following in access 2003: I have a table with the following fields: emp_id,emp_name,repdate,actions,duration,lob,category,ltstatus ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.