python - 按重要性顺序对输入进行排序
问题描述
我不是编码员,但了解 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
列表来查看列表是否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
推荐阅读
- parallel-processing - 如何避免将多个文件加载到 ODI 12c 中的单个表中的并发错误
- javascript - 事件处理程序中的错误:TypeError:无法读取 null 的属性“数据集”
- python - 如何将张量流张量转换为字节?
- firebase - 不能在颤动中使用firebase firestore的查询进行乘法运算
- reactjs - 迁移到 Mobx 6:功能组件不适用于装饰的 observables
- addeventlistener - 无法读取 null 的属性“addeventlistener”
- r - 如何在 R 中创建带有彩色单元格的水平图?
- python - 如何转发用pip下载的库?
- powerbi - 去年人数
- mysql - 如何在 SQL 中选择没有空值的行(在任何列中)?