按重要性顺序对输入进行排序

时间:2019-01-03 06:51:48

标签: python list sorting indexing

我不是编码人员,但了解python基础知识。我希望用户输入一个随机列表,例如:

Reuters
The New York Times
The Wall Street Journal
Associated Press
Financial Times

并让我的代码根据索引位置对其进行排序:

outlet = [ 'The Wall Street Journal', 'The New York Times', 'Associated Press', 'Reuters', 'Financial Times', 'USA Today','Bloomberg','CNBC']

因此它打印命令并在新行上显示:

The Wall Street Journal 
The New York Times
Associated Press
Reuters
Financial Times

谢谢!

到目前为止,我的代码如下:

 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")

1 个答案:

答案 0 :(得分:0)

好吧,因此,如果您有列表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列表并查看是否将列表{列表a具有ranking列表的第一个元素,如果有第二个元素,依此类推。该代码将是这样的:

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)

结果:

The Wall Street Journal
The New York Times
Associated Press
Reuters
Financial Times