簡體   English   中英

按重要性排序輸入

[英]Sort an input by order of importance

我不是編碼員,但了解 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")

好的,如果您有列表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列表來做,看看列表是否aranking列表的第一個元素,如果有第二個,依此類推。 代碼將是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM