Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python generate c array

def to_c_array(values, ctype="float", name="table", formatter=str, colcount=8):
    # apply formatting to each element
    values = [formatter(v) for v in values]

    # split into rows with up to `colcount` elements per row
    rows = [values[i:i+colcount] for i in range(0, len(values), colcount)]

    # separate elements with commas, separate rows with newlines
    body = ',
    '.join([', '.join(r) for r in rows])

    # assemble components into the complete string
    return '{} {}[] = {{
    {}}};'.format(ctype, name, body)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #generate #array
ADD COMMENT
Topic
Name
2+6 =