This question already has answers here:
Is there a built in function for string natural sort?
(23 answers)
Closed 8 years ago.
i'm using glob.glob() to get a list of files from a directory.
the result is this list. there is a way i can sort it using the int part of the filename?
['export_p-01.xml',
'export_p-02.xml',
'export_p-03.xml',
'export_p-04.xml',
'export_p-05.xml',
'export_p-06.xml',
'export_p-07.xml',
'export_p-08.xml',
'export_p-09.xml',
'export_p-10.xml',
'export_p-100.xml',
'export_p-101.xml',
'export_p-102.xml',
'export_p-103.xml',
'export_p-104.xml',
'export_p-105.xml',
'export_p-106.xml',
'export_p-107.xml',
'export_p-108.xml',
'export_p-109.xml',
'export_p-11.xml',
]
Here you go, with a custom lambda for using as key:
In [1]: l = ['export_p-01.xml', ...]
In [2]: sorted(l, key = lambda x: int(x.split(".")[0].split("-")[-1]))
Out[2]:
['export_p-01.xml',
'export_p-02.xml',
'export_p-03.xml',
'export_p-04.xml',
'export_p-05.xml',
'export_p-06.xml',
'export_p-07.xml',
'export_p-08.xml',
'export_p-09.xml',
'export_p-10.xml',
'export_p-11.xml',
'export_p-100.xml',
'export_p-101.xml',
'export_p-102.xml',
'export_p-103.xml',
'export_p-104.xml',
'export_p-105.xml',
'export_p-106.xml',
'export_p-107.xml',
'export_p-108.xml',
'export_p-109.xml']
Related
This question already has answers here:
Convert [key1,val1,key2,val2] to a dict?
(12 answers)
Closed 8 months ago.
I'm making a Python(3) progam to read this text file:
firstval="myval"
secondval="myotherval"
and turn it into a dict.
I tried this:
lines = file.readlines().split("\n")
values = []
for line in lines:
values.append(line.split("="))
to turn the file into a list
How do i turn this:
values = ["firstval", "myval", "secondval", "myotherval"]
into this:
values = {
"firstval": "myval",
"secondval": "myotherval"
}
You can use dict() with zip():
dict(zip(values[::2], values[1::2]))
This outputs:
{'firstval': 'myval', 'secondval': 'myotherval'}
This question already has answers here:
How do I sort a dictionary by value?
(34 answers)
Closed 2 years ago.
I have a variable.
Score = {}
After some calculation, the value of Score is :
{
'449 22876': 0.7491997,
'3655 6388': 0.99840045,
'2530 14648': 0.9219989,
'19957 832': 0.9806836,
'2741 23293': 0.64072967,
'22324 7525': 0.986661,
'9090 3811': 0.90206504,
'10588 5352': 0.8018138,
'18231 7515': 0.9991332,
'17807 14648': 0.9131582
.....
}
I want to sort it by the third value(e.g. 0.7491997).
I only want to get the top 100 high score.
How can I do?
if you want to sort the dictionary by the values of the dictionary (which is what I am getting from your question) you could do it with this lambda function:
sorted_dict = sorted(score.items(), key=lambda x: x[1])
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
d={'a0': [['5021', '5031'], ['4994', '4991', '5042'],
['4992', '4995', '5021', '4994'], ['5037', '5038']],
'a24': [['5009', '5014'], ['5009', '5014'], ['4993', '4998', '5030', '4991']]
}
I have the above dict in python.
I need to make list with the name of it being the names of keys in the dict.
The list with the name of the key should have the items as its corresponding values in dict.
The output should be:
a0=[['5021', '5031'], ['4994', '4991', '5042'],
['4992', '4995', '5021', '4994'], ['5037', '5038']]
a24=[['5009', '5014'], ['5009', '5014'], ['4993', '4998', '5030', '4991']]
Any help is appreciated.
First, rethink if this is really necessary. Dynamically creating variables is confusing and does not occur often. It would be better to avoid this.
However, you can do it like this:
for name, val in d.items():
exec("{}={}".format(name,val))
for key, item in d.items():
print(str(key) + "=" + str(item))
This question already has answers here:
Replacing multiple similar strings
(1 answer)
How to replace two things at once in a string?
(6 answers)
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I am trapped in a quite straight forward problem, but after some tweak, I simply cannot find an easy and efficient algorithm to do this.
So basically I have a string, which contains certain elements, for instance:
l = "test %1, %13, %14, %15"
And a map:
dict = {"%1": "%33", "%13": "%14", "%14", "%15", "%15": "%17"}
And I would like to do the following stuff:
for k in dict.keys():
l = l.replace(k, dict[k])
So what I am expecting is:
l = "test %33, %14, %15, %17"
But apparently this is not feasible, since there are some conflict between keys and values. So the above code would output:
l = "test %33, %17, %17, %17"
Sorry for such native problem, but how should I solve this and get my expected output? Note that the length of each key/value is not fixed, so I cannot do something like:
m_idx = l_copy.find(key)
l = l[:m_idx] + dict[key] + l[m_idx+len(key):]
Because the length is not fixed, the above code is still buggy.
Am I clear on this? Any suggestion would be appreciated very much!
======= update
So all keys follow this pattern of %[0-9]+.
You can use re.sub with a lambda:
import re
l = "test %1, %13, %14, %15"
_dict = {"%1": "%33", "%13": "%14", "%14":"%15", "%15": "%17"}
new_l = re.sub('%\d+', lambda x:_dict[x.group()], l)
Output:
'test %33, %14, %15, %17'
You can use dict.get to prevent a KeyError by providing a default value should the key not be present in _dict:
new_l = re.sub('%\d+', lambda x:_dict.get(x.group(), x.group()), l)
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 5 years ago.
I am having a function in python
def func(dataFrame,country,sex):
varible_name=dataFrame[(dataFrame['land']==country) & (dataFrame['sex']==sex)]
Now, for example, I call this function
func(dataFrame,'England','M')
I want that variable name be England_M instead of variable_name.
You can't do that in Python.
What you can do instead is store the results under a dictionary with key = England_M for instance.
In your case, you could do the following :
def func(dataFrame,country,sex):
tmp = dataFrame[(dataFrame['land']==country) & (dataFrame['sex']==sex)]
variable_name = "{c}_{s}".format(c=country, s=sex)
return dict(variable_name=tmp)
Now using it :
results = func(dataFrame, "England", "M")
print(results['England_M'])