开发者

How do I concatenate strings from a dictionary by identifying the last item with Python?

开发者 https://www.devze.com 2023-01-06 15:15 出处:网络
I need to concatenate string to an existing one as follows. for k,v in r.iteritems(): tableGenString += \"%s %s, \" % (k, what_type(v))

I need to concatenate string to an existing one as follows.

for k,v in r.iteritems():
    tableGenString += "%s %s, " % (k, what_type(v))

The problem is that for the last item the comma(',') should not be added.

How can I check if k,v is the last item?

开发者_开发技巧Added

The example is a simplified version of the real code as follows.

for k,v in r.iteritems():
    filteredKey = m.mapper(k)
    (typestring, valuestring) = what_type(v)
    tableGenString += "%s %s, " % (k, typestring)
    string1 += "%s, " % k
    string2 += "%s, " % valuestring

I need to check the last item for this case.


Don't build large strings with concatenation like that. Do this instead:

tableGenString = ', '.join('%s %s' % (k, what_type(v)) for k, v in r.iteritems())


The OP insists in a comment:

I need to find a way to find the last item as I added to my original question

apparently for purposes other than clumsily duplicate the ', '.join correctly suggested in other answers.

Of course, the order in which keys and values are presented by iteritems (or any other way to iterate on a dict, which is a hash table and has no concept of "order", and therefore not of "first" or "last" either!) is totally arbitrary. Nevertheless, the last one (to be presented in this arbitrary order) can be identified:

for i, (k,v) in enumerate(r.iteritems()):
    if i == len(r) - 1:
        print "last item: key=%r, value=%r" % (k, v)

Similarly, once you get i from this kind of enumerate call, if i==0: will identify the first item, if i==len(r)//2: will identify the middle item, and so forth.


Use the "join" string method instead of an outter for:

tableGenString = ", ".join ("%s %s" %(k, what_type(v) for k, v in r.iteritems())


Ugh. If you'd like something that's readable instead of an overly-dense join, here:

def build_str(r):
    tableGenString = ""
    for k,v in r.iteritems():
        if tableGenString != "":
            tableGenString += ", "
        tableGenString += "%s %s" % (k, type(v))

    return tableGenString

Sample return value:

baz <type 'str'>, have a list <type 'list'>, foo <type 'int'>, bar <type 'str'>, or a tuple <type 'tuple'>

And lest someone complain that this is somehow slower than the join... it's not on my box:

import timeit

r = {
    "foo": 123,
    "bar": "456",
    "baz": "abcd",
    "have a list": [7, 8, "efgh"],
    "or a tuple": (9, 0, 10),
}

def build_str(r):
    tableGenString = ""
    for k,v in r.iteritems():
        if tableGenString != "":
            tableGenString += ", "
        tableGenString += "%s %s" % (k, type(v))

    return tableGenString

if __name__ == '__main__':
    readable_time = timeit.Timer("build_str(r)", "from __main__ import build_str, r").timeit()
    print "Readable time: %f" % (readable_time,)

    join_time = timeit.Timer("', '.join('%s %s' % (k, type(v)) for k, v in r.iteritems())", "from __main__ import r").timeit()
    print "Join time: %f" % (join_time,)

...

$ python concat.py 
Readable time: 4.705579
Join time: 5.277732
$ 
0

精彩评论

暂无评论...
验证码 换一张
取 消