[英]Sort an input by order of importance
I am not a coder but understand python basics.我不是编码员,但了解 Python 基础知识。 I want my user to input a random list such as:我希望我的用户输入一个随机列表,例如:
Reuters
The New York Times
The Wall Street Journal
Associated Press
Financial Times
and for my code to sort it based on the index postion:并让我的代码根据索引位置对其进行排序:
outlet = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']
so it prints ordered and on a new line:所以它打印有序并在新行上:
The Wall Street Journal
The New York Times
Associated Press
Reuters
Financial Times
Thank you!谢谢!
The code I have so far is as follows:我到目前为止的代码如下:
ranking = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']
print('Enter new coverage:')
coverage = input()
listed = coverage.split()
print(*listed, sep="\n")
Ok, so if you have the list a = [Reuters, The New York Times, The Wall Street Journal, Associated Press, Financial Times]
and you want to to sort it with respect to list ranking = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']
you can do it by iterating through the ranking
list and see if the list a
has the first element of the ranking
list, if it has the second and so on.好的,如果您有列表a = [Reuters, The New York Times, The Wall Street Journal, Associated Press, Financial Times]
并且您想根据列表ranking = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']
对其进行排序ranking = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']
你可以通过遍历ranking
列表来做,看看列表是否a
有ranking
列表的第一个元素,如果有第二个,依此类推。 The code would be something like this:代码将是这样的:
a = [Reuters, The New York Times, The Wall Street Journal, Associated Press, Financial Times]
ranking = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']
for element in ranking:
for i in a:
if i == element:
print(i)
Result:结果:
The Wall Street Journal
The New York Times
Associated Press
Reuters
Financial Times
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.