👻 See our latest reviews to choose the best laptop for Machine Learning and Deep learning tasks!
What"s the easiest way to use a linked list in python? In scheme, a linked list is defined simply by "(1 2 3 4 5)
. Python"s lists, [1, 2, 3, 4, 5]
, and tuples, (1, 2, 3, 4, 5)
, are not, in fact, linked lists, and linked lists have some nice properties such as constant-time concatenation, and being able to reference separate parts of them. Make them immutable and they are really easy to work with!
👻 Read also: what is the best laptop for engineering students in 2022?
Python Linked List sep: Questions
How to print number with commas as thousands separators?
5 answers
I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567
as 1,234,567
. How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way.
It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably possible.
Answer #1
Locale unaware
"{:,}".format(value) # For Python ≥2.7
f"{value:,}" # For Python ≥3.6
Locale aware
import locale
locale.setlocale(locale.LC_ALL, "") # Use "" for auto, or force e.g. to "en_US.UTF-8"
"{:n}".format(value) # For Python ≥2.7
f"{value:n}" # For Python ≥3.6
Reference
Per Format Specification Mini-Language,
The
","
option signals the use of a comma for a thousands separator. For a locale aware separator, use the"n"
integer presentation type instead.
Answer #2
I got this to work:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US")
"en_US"
>>> locale.format("%d", 1255000, grouping=True)
"1,255,000"
Sure, you don"t need internationalization support, but it"s clear, concise, and uses a built-in library.
P.S. That "%d" is the usual %-style formatter. You can have only one formatter, but it can be whatever you need in terms of field width and precision settings.
P.P.S. If you can"t get locale
to work, I"d suggest a modified version of Mark"s answer:
def intWithCommas(x):
if type(x) not in [type(0), type(0L)]:
raise TypeError("Parameter must be an integer.")
if x < 0:
return "-" + intWithCommas(-x)
result = ""
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
Recursion is useful for the negative case, but one recursion per comma seems a bit excessive to me.
Python Linked List sep: Questions
How would you make a comma-separated string from a list of strings?
5 answers
What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ["a", "b", "c"]
to "a,b,c"
? (The cases ["s"]
and []
should be mapped to "s"
and ""
, respectively.)
I usually end up using something like "".join(map(lambda x: x+",",l))[:-1]
, but also feeling somewhat unsatisfied.
Answer #1
my_list = ["a", "b", "c", "d"]
my_string = ",".join(my_list)
"a,b,c,d"
This won"t work if the list contains integers
And if the list contains non-string types (such as integers, floats, bools, None) then do:
my_string = ",".join(map(str, my_list))