I have a list like this:
[
'C:\\Users\\Rash\\Downloads\\Programs\\a.txt',
'C:\\Users\\Rash\\Downloads\\a.txt',
'C:\\Users\\Rash\\a.txt',
'C:\\Users\\ab.txt',
'C:\\Users\\aa.txt'
]
and I want to sort it based on two conditions:
Sort by no of "\" present in the string.
By Alphabets.
My end result should be this:
[
'C:\\Users\\aa.txt',
'C:\\Users\\ab.txt',
'C:\\Users\\Rash\\a.txt',
'C:\\Users\\Rash\\Downloads\\a.txt',
'C:\\Users\\Rash\\Downloads\\Programs\\a.txt'
]
I am just learning lambda function in python and wrote this code:
print(sorted(mylist, key=lambda x:x.count("\\")))
but this code only sorts by count of "\". It does not sort it by alphabets. The result is that I am seeing the key "'C:\Users\ab.txt'" before the key "'C:\Users\aa.txt'".
I could sort the list two times, but I want to do it in one line. What should I add in the lambda code ? Since I am new to this whole "lambda" thing, I cannot think of a way to do this. Thanks for replying!! :)
Return a sequence from the key function that contains the items you want to sort by.
key=lambda x: (x.count('\\'), x.split('\\'))
Related
I am newbie to Python . Need help in joining/merging the output of a for loop which creates multiple dictionaries into one single big dictionary .
vpc_peer = {}
for key in vpc_list:
for value in neigh_ips:
vpc_peer[key] = value
neigh_ips.remove(value)
break
print(vpc_peer)
The print output looks like this
{'vpc-0a21601da78d1ef30': '169.254.27.159'}
{'vpc-049920287c06fb681': '169.254.27.203'}
{'vpc-0f3bc5629259fb713': '169.254.28.23'}
{'vpc-0b0e5a878de4ca0b3': '169.254.28.27'}
What I am looking for is to merge the output of the for loop into one big dictionary as below
{'vpc-0a21601da78d1ef30': '169.254.27.159','vpc-049920287c06fb681': '169.254.27.203', 'vpc-0f3bc5629259fb713': '169.254.28.23','vpc-0b0e5a878de4ca0b3': '169.254.28.27' }
I think this question has many possible answers to your problem. A quick way to do this is just to use the update() function:
vpc_peer.update(neigh_ips)
I got this assignment for school. The task is: which of the componist comes first in alphabetical order using Python 3. I tried to look up online but couldn't find anything, would appreciate some help. :)
Componists: 'Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Bernstein'
Thanks in advance.
You can use comparison operators to check for the alphabetical order in strings, too:
'Berlioz' < 'Borodin'will return True, while 'Berlioz' > 'Borodin'will return False
Since the list.sort() method uses comparison of the list's components, it is as simple as this:
composers = ['Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Berns
tein']
composers.sort()
print(composers)
If you store the names of the componist in a list and use the sorted function, you can get your sorted list
componist=list(['Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Bernstein'])
sorted_componist=sorted(componist)
print(sorted_componist)
This prints
['Bartok', 'Bellini', 'Berlioz', 'Bernstein', 'Borodin', 'Brian', 'Buxtehude']
The following is my list
List_data = [{'cases_covers': 0.1625}, {'headphone': 0.1988}, {'laptop': 0.2271}, {'mobile': 0.2501}, {'perfume': 0.4981}, {'shoe': 0.1896}, {'sunglass': 0.1693}]
Final answer should be like this:
[{'perfume': 0.4981}, {'mobile': 0.2501}, {'laptop': 0.2271}, {'headphone': 0.1988},{'shoe': 0.1896}, {'sunglass': 0.1693},{'cases_covers': 0.1625}]
i want them to be sorted based on the value of the key in descending order
You can get the list of values of a dictionary d with d.values(). Since your dictionaries have only one entry each, these lists will be singletons. You can use these singleton-lists to sort List_data by supplying a keyword argument to the sort function.
Please note that in your example, "perfume", "mobile", "laptop" are the keys and 0.4981, 0.2501 are the values, according to the standard vocabulary for dictionaries in python.
List_data = [{'cases_covers': 0.1625}, {'headphone': 0.1988}, {'laptop': 0.2271}, {'mobile': 0.2501}, {'perfume': 0.4981}, {'shoe': 0.1896}, {'sunglass': 0.1693}]
List_data.sort(key=lambda d: list(d.values()), reverse=True)
print(List_data)
Output:
[{'perfume': 0.4981}, {'mobile': 0.2501}, {'laptop': 0.2271}, {'headphone': 0.1988}, {'shoe': 0.1896}, {'sunglass': 0.1693}, {'cases_covers': 0.1625}]
Important remark
The previous piece of code was answering your question literally, without knowledge of the context in which you are trying to sort this list of dictionaries.
I am under the impression that your use of lists and dictionaries is not optimal. Of course, without knowledge of the context, I am only guessing. But perhaps using only one dictionary would better suit your needs:
dictionary_data = {'cases_covers': 0.1625, 'headphone': 0.1988, 'laptop': 0.2271, 'mobile': 0.2501, 'perfume': 0.4981, 'shoe': 0.1896, 'sunglass': 0.1693}
list_data = sorted(dictionary_data.items(), key=lambda it: it[1], reverse=True)
print(list_data)
Output:
[('perfume', 0.4981), ('mobile', 0.2501), ('laptop', 0.2271), ('headphone', 0.1988), ('shoe', 0.1896), ('sunglass', 0.1693), ('cases_covers', 0.1625)]
How can I convert a list that has entries like "MAX_PAYLOAD NVARCHAR(5)" to a dictionary that contains key value pairs like "MAX_PAYLOAD" : "NVARCHAR(5)" ?
Assuming that this string is representative of what you're going to see, you might be able to simply split on the space in the middle to create a tuple, then create a dictionary from those tuples.
lines = [
"MAX_PAYLOAD NVARCHAR(5)",
"MIN_PAYLOAD NVARCHAR(4)",
]
broken_lines = [line.split(maxsplit=1) for line in lines]
assert dict(broken_lines) == {'MAX_PAYLOAD': 'NVARCHAR(5)', 'MIN_PAYLOAD': 'NVARCHAR(4)'}
If you might have other whitespaces or something, figure out a different splitting function that'll work for you.
I am trying to sort a list that contain in each index an integer and a string. like the one in the example.
I used sort() and split but I get always the wrong ordered that I expect
def takeSecond(elem):
return elem.split("|")[2]
list = ['|val1: 0|0','|val: 0|80','|val1 0|140','|val1: 0|20','|val1: 0|90']
list.sort(key=takeSecond)
print(list)
that returns:
['|val1: 0|90','|val: 0|80','|val1: 0|20','|val1: 0|0','|val1 0|140']
and I expect to get this:
['|val1: 0|140','|val: 0|90','|val1: 0|80','|val1: 20|0','|val1 0|0']
Where is my mistake in here?
Try this:
l = ['|val1: 0|0','|val: 0|80','|val1 0|140','|val1: 0|20','|val1: 0|90']
l.sort(key=lambda x:int(x.rsplit('|')[-1]), reverse=True)
This will sort your list based on what you need. and the expected output is:
In [18]: l
Out[18]: ['|val1 0|140', '|val1: 0|90', '|val: 0|80', '|val1: 0|20', '|val1: 0|0']
In addition note that:
Do not use list as a variable name. list is a built-in name in python, you will override its functionality .