Equivalent of Paste R to Python Equivalent of Paste R to Python python python

Equivalent of Paste R to Python


This very much works like Paste command in R:R code:

 words = c("Here", "I","want","to","concatenate","words","using","pipe","delimeter") paste(words,collapse="|")

[1]

"Here|I|want|to|concatenate|words|using|pipe|delimeter"

Python:

words = ["Here", "I","want","to","concatenate","words","using","pipe","delimeter"]"|".join(words)

Result:

'Here|I|want|to|concatenate|words|using|pipe|delimeter'


Here's a simple implementation that works on lists, and probably other iterables. Warning: it's only been lightly tested, and only in Python 3.5+:

from functools import reducedef _reduce_concat(x, sep=""):    return reduce(lambda x, y: str(x) + sep + str(y), x)        def paste(*lists, sep=" ", collapse=None):    result = map(lambda x: _reduce_concat(x, sep=sep), zip(*lists))    if collapse is not None:        return _reduce_concat(result, sep=collapse)    return list(result)assert paste([1,2,3], [11,12,13], sep=',') == ['1,11', '2,12', '3,13']assert paste([1,2,3], [11,12,13], sep=',', collapse=";") == '1,11;2,12;3,13'

You can also have some more fun and replicate other functions like paste0:

from functools import partialpaste0 = partial(paste, sep="")

Edit: here's a Repl.it project with type-annotated versions of this code.


For this particular case, the paste operator in R is closest to Python's format which was added in Python 2.6. It's newer and somewhat more flexible than the older % operator.

For a purely Python-ic answer without using numpy or pandas, here is one way to do it using your original data in the form of a list of lists (this could also have been done as a list of dict, but that seemed more cluttered to me).

# -*- coding: utf-8 -*-names=['categorie','titre','tarifMin','lieu','long','lat','img','dateSortie']records=[[    'zoo',   'Aquar',     0.0,'Aquar',2.385,48.89,'ilo',0],[    'zoo',   'Aquar',     4.5,'Aquar',2.408,48.83,'ilo',0],[    'lieu',  'Jardi',     0.0,'Jardi',2.320,48.86,'ilo',0],[    'lieu',  'Bois',      0.0,'Bois', 2.455,48.82,'ilo',0],[    'espac', 'Canal',     0.0,'Canal',2.366,48.87,'ilo',0],[    'espac', 'Canal',    -1.0,'Canal',2.384,48.89,'ilo',0],[    'parc',  'Le Ma',    20.0,'Le Ma', 2.353,48.87,'ilo',0] ]def prix(p):    if (p != -1):        return 'C  partir de {} €uros'.format(p)    return 'sans prix indique'def msg(a):    return 'Evenement permanent  --> {}, {} {}'.format(a[0],a[1],prix(a[2]))[m.append(msg(m)) for m in records]from pprint import pprintpprint(records)

The result is this:

[['zoo',  'Aquar',  0.0,  'Aquar',  2.385,  48.89,  'ilo',  0,  'Evenement permanent  --> zoo, Aquar C  partir de 0.0 \xe2\x82\xacuros'], ['zoo',  'Aquar',  4.5,  'Aquar',  2.408,  48.83,  'ilo',  0,  'Evenement permanent  --> zoo, Aquar C  partir de 4.5 \xe2\x82\xacuros'], ['lieu',  'Jardi',  0.0,  'Jardi',  2.32,  48.86,  'ilo',  0,  'Evenement permanent  --> lieu, Jardi C  partir de 0.0 \xe2\x82\xacuros'], ['lieu',  'Bois',  0.0,  'Bois',  2.455,  48.82,  'ilo',  0,  'Evenement permanent  --> lieu, Bois C  partir de 0.0 \xe2\x82\xacuros'], ['espac',  'Canal',  0.0,  'Canal',  2.366,  48.87,  'ilo',  0,  'Evenement permanent  --> espac, Canal C  partir de 0.0 \xe2\x82\xacuros'], ['espac',  'Canal',  -1.0,  'Canal',  2.384,  48.89,  'ilo',  0,  'Evenement permanent  --> espac, Canal sans prix indique'], ['parc',  'Le Ma',  20.0,  'Le Ma',  2.353,  48.87,  'ilo',  0,  'Evenement permanent  --> parc, Le Ma C  partir de 20.0 \xe2\x82\xacuros']]

Note that although I've defined a list names it isn't actually used. One could define a dictionary with the names of the titles as the key and the field number (starting from 0) as the value, but I didn't bother with this to try to keep the example simple.

The functions prix and msg are fairly simple. The only tricky portion is the list comprehension [m.append(msg(m)) for m in records] which iterates through all of the records, and modifies each to append your new field, created via a call to msg.