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']
Related
Hi I have a list of places
>>> places = ['NYC','PA', 'SF', 'Vienna', 'Berlin', 'Amsterdam']
I temporarily sort it with
>>> sorted(places)
And finally I want to sort the sorted(places) list in reverse alphabetical order.
Is this ever correct?
>>> sorted(places).reverse()
I thought it was but Python3 says the list is none.
Thank you
here are a few examples how you can do it:
your list:
places = ['NYC','PA', 'SF', 'Vienna', 'Berlin', 'Amsterdam']
using sort method with reverse parameter
places.sort(reverse=True)
print(places)
using sorted and reverse methods on a new variable since sorted method would require one:
testlist=sorted(places)
testlist.reverse()
print(testlist)
using sorted method with reverse parameter:
testlist1=sorted(places, reverse=True)
print(testlist1)
print(sorted(places,reverse=True))
or you can use a variable
other_list=sorted(places,reverse=True)
print(other_list)
This one is a bit tricky. I have a list: list_a=[5.,4.,2.,6.] and i want to order this list by ascending but also do the same ordering to another list: list_b=[left,up,right,down]. The output should be:
list_a=[2.,4.,5.,6.]
list_b=[right,up,left,down]
In reality the lists are huge and variable but have the same len (list_a is though always number and a dot). I want to copy the ordering of the list_a to list_b.
Thanks!
You can zip and sorted for this,
sorted_list_b = [x for _,x in sorted(zip(list_a,list_b))]
print(sorted(list_a))
print(sorted_list_b)
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 .
I have a list of 20000 Products with their Description
This shows the variety of the products
I want to be able to write a code that searches a particular word say 'TAPA'
and give a output of all the TAPAs
I found this Find a specific word from a list in python , but it uses startswith which finds only the first item for example:
new = [x for x in df1['A'] if x.startswith('00320')]
## output ['00320671-01 Guide rail 25N/1660', '00320165S02 - Miniature rolling table']
How shall i find for the second letter, third or any other item
P.S- the list consists of strings, integers, floats
You can use string.find(substring) for this purpose. So in your case this should work:
new = [x for x in df1['A'] if x.find('00320') != -1]
The find() method returns the lowest index of the substring found else returns -1.
To know more about usage of find() refer to Geeksforgeeks.com - Python String | find()
Edit 1:
As suggested by #Thierry in comments, a cleaner way to do this is:
new = [x for x in df1['A'] if '00320' in x]
You can use the built-in functions of Pandas to find partial string matches and generate lists:
new = df1['A'][df1['A'].astype(str).str.contains('00320')]['A'].tolist()
An advantage of pandas str.contains() is that the use of regex is possible.
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('\\'))