- Changing the starting index
please, add a system variable like ‘set_index = 0’ or ‘set_index = 1’ to choose what will be the starting index in lists, tuples, range()… - Formatting numbers
please, add a system variable like number_format = ‘,.’ to format numbers like 123,456,789.00 and number_format = ‘.,’ to format numbers like 123.456.789,00 - Including the last number in range()
please, add option to include the last number in range() function. Examples: range(9] includes number 9 in loops so it will loop from 0 to 9 - Sorting dictionaries
my_dict.sort(‘key’) or my_dict.sort(‘value’) instead of messing with lambda or such. Much easier to understand and use.
For 1, you need to study more.
The index is the offset of the vector. On the first element, it has no offset so the start number is 0.
For 2, you can use it:
a = 123_456_789.00
For 3,
for i in range(9)
If it starts with 0 and ends with 9, it repeat 10 times.
For 4, use
getattr(my_dict, "keys"), getattr(my_dict, "values")
- I would like to change starting index from 0 to 1.
- My suggestion is about formatting number in text, like: f’ my number is {number}:.’ to get 123.456.789,00 instead of 123,456,789.00
- I know it will repeat 10 times with () butt I want to include number 9 in this case to repeat 11 times with (]
- I want to sort dictionaries easier way, not to get info.
1 is seems to be impossible.
2 why do you need a number like that?
3 It becomes more complex. The other people need more times to candicate the repeat times.
4 Now the implement in python is simple enough. Add a new way is uneless.:
sorted(my_dict.keys())
sorted(my_dict.values())
- I am not trying to do that. I suggest an idea.
- Because I live in Macedonia (Europe), not in US.
- Also, I am trying to suggest an idea.
- Up to now I can do this with lambda or by converting to lists, or… I can’t find easier way so I am also suggesting an idea.
There is a way for 2:
import locale
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
x = 123456789.00
formatted = locale.format_string("%.2f", x, grouping=True)
print(formatted) # 123.456.789,00
The way I suggested is found in PowerBASIC years ago. I found it much easier to understand and use instead of using locales. And it will fit any language instead of only one.
Additional suggestions:
- Make strings mutable so text[5] = ‘abc’ will change the text directly instead of using replace or other string functions.
- Make sets keep their position any time they are called. This way they can be accessed with indexes instead by names.
- How can strings still be used as dict keys then?
- What do you mean with accessing sets with names?
-
You are not going to change those strings used in dictionary.
-
Well, when you want to delete an element in set you can’t do it by an index like delete a[5]. You may delete wrong element. You have to use its name. Also, if you try to print my_set[5] you will always get another value.
- You can now anyway change a string using replace or some other string functions. My suggestion is to make it easier.
Perhaps Python isn’t the right language for you. Have you considered designing your own language, perhaps one that compiles to Python for simplicity? You can build your own parser and then output runnable code. It sounds like you have very strong opinions about what a language should do.
Maybe it is not the right language for me, but it is not for the others that make suggestions like I do, too.
Of course I have strong opinion what a language should do. I am in this business for decades.
How? What even is the “name” of the element 14 in the set {3, 14}, for example?
If I use a object like it. Tell me what the name it is:
{object.__new__(type(exit))}
Sorry, wrong term used. “Item” instead of “name”. So, to delete an element you can use ‘remove’ or ‘discard’.
On changing the starting index - I would strongly vote against adding this as it becomes a debugging nightmare (Perl has this feature and most Perl friends would never use it).
On default formatting of floats - the format could be assigned to a variable and incorporated in f-strings. I have not fealt the need to add a (module level) global default float format myself.
Do not include the last number in a range()! There are advantages in how it is, including:
- the length of the range is simply the upper-bound minus the lower bound.
- It dovetails with our zero-based array indexing: an N-element array has indexes from 0 to N-1, i.e.
range(N). - Partitioning a range into sub ranges: given a range(N) and a splitting point S < N then the split ranges are simple range(S) and range(S, N) without overlap. The ending bound of the first range is exactly the starting bound of the second range.
Sorting dictionaries: ???
It’s usually only one idea that gets proposed at a time, not 6 (or 7?). And if an idea is proposed, it still takes a lot of patience to adapt it.
Changing some of the most used, and simplest features of the language, just because you feel like it would make working code break for millions (ik not kidding) of people. Python is one of the most used languages in the world, so every single change needs to be viewed from the point of others.
Perhaps implementing some of these things for yourself is possible? Overwriting list and other indexable objects is possible, and adding those new types to your scope at runtime is possible too. Implementing some other functionalities you mentioned can also work, but some, e.g. the range notation will simply raise an error.
- Impossible. Where would you define the starting index and what scope would it apply to? If it only applies to the code you write, why not subclass the builtins and delegate to the parent class methods with that offset? Although this still adds a great maintenance burden to people who work with you, because you must tell them of your imposing convention, which actually makes much sense when you consider how an array is represented in lower-level languages (as a contiguous block of memory, the first item having offset 0 from the reference pointer, which is the first item).
- The only discussable suggestion IMO, but subject to the same scope considerations. I would change it to be in the form of a string format code like the “#x” in
f'{id(obj):#x}', because that requires no syntax-level modifications, but that would deviate from the Cscanfandprintfcodes further. - Impossible, since it requires a backwards-incompatible language-level change. Parsing ambiguities arise, all hell breaks loose. What if the
rangename is re-assigned to? Are you proposing to makerangea new type of keyword, breaking all code that has a variable calledrange? Anyway, you can accomplish what you want byrange(end+1). - Out of the question. Dictionaries are not meant to be sorted. Iterating over them gives items in insertion order because its underlying representation is a hash table. Do you know what that is? Can you name me one use case where you would need the lookup and membership behaviour of a
dictbut the sorting behaviour of a list? How often will you sort the dictionary by variably the keys or values (since each pass is O(n log n) time)? If you can’t, use a list of tuples! Even if you can, why not use this? - Use a
bytearrayorio.StringIO. You remarked that the strings used as dictionary keys will be made immutable. How? By reference counting, which will incur a great performance penalty for a stupid feature? You said you can change a string now. No you cannot! All the string methods create a new, equally immutable string! - What if an item in the middle is removed? What happens to the indices behind? If you want that functionality, use this!
You have been very dismissive to replies for how terrible and inconsiderate your suggestions were. I hope you read through this reply carefully.
But that already exists (at least one version):
x = 123456789
print(f'{x:,.2f}')
Gives me 123,456,789.00
This doesn’t make sense. Before 3.6, it was of course already a hash table, and items were not in insertion order (and I’d say that was because it was a hash table).