This question already has answers here:
Is there a built in function for string natural sort?
(23 answers)
Python analog of PHP's natsort function (sort a list using a "natural order" algorithm) [duplicate]
(3 answers)
Closed 5 years ago.
I have a directory with 1600 photos and I need to save the path to each foto to a list and then to .txt file.
The photos are enumerated according to the position they should have in the list: img(0), img(1)... and I need this position to be kept.
What I obtain is this order, so now in list index 2 I have img(10):
img(0) img(1) img(10) img(100) img(1000) img(1001)...
img(2) img(2) img(20) img(200) img(2000) img(2001)...
Apparently, I'm the only one having this issue because I didn't find any discussion about this problem. Thank you very much for helping me.
As mentioned by others, the documentation does not guarantee any particular ordering. In your case it appears to be sorted alphabetically/lexicographically. "10" comes before "2" alphabetically. You'll have to prepend 0s to give every file the same number of digits to get the ordering you want if this behaviour appears to remain consistent on your machine.
For example, "002" will come before "010".
If you want to be safe (for example need to be able to port your code to other machines/OSes), you'll want to manually sort.
Related
This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 4 years ago.
I am working on python script that splits text in different blocks based on keywords used in text.
Currently I split text into blocks with sth like this (for 1 block, others have pretty much the same strucure):
if (line.strip().lower().startswith('ключевые навыки')
or line.strip().lower().startswith('дополнительная информация')
or line.strip().lower().startswith('знания')
or line.strip().lower().startswith('личные качества')
or line.strip().lower().startswith('профессиональные навыки')
or line.strip().lower().startswith('навыки')):
But, it is possible that list of keywords is going to expand. Is there a possibility to generate multiple or statements based on some array of possible keywords?
Try this code
values=['ключевые навыки','дополнительная информация','знания']
val=True
#enter any words you want to check
while val
for i in values:
if (line.strip().lower().startswith(i)):
#whatever code you want to implement
val=False
#to exit loop
Hope it helps :)
This question already has answers here:
IPython Notebook output cell is truncating contents of my list
(9 answers)
Closed 6 years ago.
I have a dictionary with around 1200 key-valuepairs. I want to be able to go through all of them to check the values, but it cuts off at 1000 (and ends on ...}).
I expect there some setting somewhere to print all of them? I'm doing this in jupyter notebook using python 2.7.
I'm doing Flux Balance Analysis on a model, and the goal is to identify fluxes that does not work - but I find that the value of these fluxes are not necessarily 0, but can simply be an extremely low number.
EDIT: I've just been typing the dict directly into the command line. The function that I used to generate the dict also returned it by default, and that got cut off as well.
Using print str(dict) works... But then it becomes unreadable because it's all on one line instead of displaying one key-value pair per line.
Iterating over it with a 'for i in dict, print i, dict[i]' worked though, thanks!
I think the answer you're looking for is available in this old question here: IPython Notebook output cell is truncating contents of my list
This question already has answers here:
How can I get the next string, in alphanumeric ordering, in Python?
(4 answers)
Closed 6 years ago.
If Python has an implementation of Ruby's next method? I mean something what works exactly the same as in Ruby, so if I type e.g. "z".next it will return "aa" (instead of just next sign in ascii table), "az".next will return "ba" and so on.
I don't believe there is a built-in method for this in Python. A similar question was asked on How can I get the next string, in alphanumeric ordering, in Python? and the accepted answer gives a solution.
This question already has answers here:
Why is the order in dictionaries and sets arbitrary?
(5 answers)
Closed 6 years ago.
I am a beginner in python and I am using python 2.4.3.
I have a question regarding to the order resulted from the set()function.
I understand set() will remove the the duplicate elements from a string and
[class set([iterable])
Return a new set object, optionally with elements taken from iterable.]1
But for example, when I do the following
a='abcdabcd'
set(a)
it returned a result of
set(['a','c','b','d'])
in stead of
set(['a','b','c','d'])
which I would actually expect.
Why is that? I am not able to understand how the output was generated.
Many thanks in advance.
A set is defined as an "unordered collection of unique elements" (see here). The set object in Python make no guarantees about ordering, and you should not expect nor rely on the order to stay the same.
This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.