How do I join a list of integers?

TimeWillTell

My code's purpose is to list all possible 4 digit numbers (0000-9999), which it does easily.

i = 0
gen = []
while i <= 9999: 
    gen.append(i)
    i = i + 1

for cycle in gen:
    format(cycle, '04')

However, it prints each number on its own line, and I would like it to print in one big string. So my question is how do I join my list of integers so that it prints in one big string? I'm running Python 3.3 on Windows.

Blorgbeard

So, there is a function range, which builds a generator for you; for example: range(0,10000) will generate 10,000 integers starting from 0 (0 to 9999 inclusive).

Note the range object is not a list, it's a function that will generate a list. You could make a list by doing:

list(range(0,10000))

Which gets you something like

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 9999]

You can use a list comprehension to format each entry in the list:

[format(x, '04') for x in range(0,10000)]

Which gives you a list of strings like:

['0000', '0001', '0002', '0003', '0004', ...

Then to join a list of strings together, there is a function called join. You want to put the empty string between each number, so you call it like this:

"".join([format(x, '04') for x in range(0,10000)])

And you get your output:

00000001000200030004000500060007 ... 9999

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I cast a Java Object into a List of integers?

How do I filter a stream of integers into a list?

How do I divide an ordered list of integers into evenly sized sublists?

How do I split a list of integers into a list of digit integers?

How do I initialize an integer array in LLVM using a list of integers?

How to join list of integers into one integer python

How do I add all integers in one list to integers on another list with different length in the same order in Python?

How do I parse a JSONObject into a list of integers?

How do I sum all the items of a list of integers in Kotlin?

How do I choose several maximum values from a list of integers?

How do I join individual elements into a list of strings in Python

How do I recursively split a linked list of integers into an odd and even list in C++?

how do I join multiple list with varargs in java?

How do I define a variable which is a sequential list of integers in gmake?

How do I find the max from a list of integers

How do I convert a list of integers into bytes?

How do I join a list and then delete the last character?

How do I flatten a python list? Inputs are lists, strings or integers

How do i get the list of people not in an inner join?

How do I access integers within an element of a list?

How do I join a list of values into a Python dictionary?

How do I convert a list of integers to a string - Python

How do I convert a list of strings to integers in Python

How do I feed a list of integers into SQL and it returns a dataframe of items

How do I convert a list of integers into an actual list in python

How do I distribute a value across a list of integers, with a priority to fill the integers to a soft-cap first

How do I split a list of a single string containing multiple numbers into a list of separated integers?

Python: How do I put a list comprehension in a join function?

How do I join the following list of series based on series name?