2 回答
TA贡献2003条经验 获得超2个赞
headlines = [['In bid to boost realty, state cuts stamp duty for 7 mths ', '42'],
['India sees world’s third-biggest spike of 76,000+ cases, toll crosses 60k ','28'],
['O', '33'],
['Don’t hide behind RBI on loan interest waiver: SC to govt ', '28']]
for idx, line in enumerate(sorted([row for row in headlines if len(row[0]) > 1], key=lambda z: int(z[1]), reverse=True)):
print("{}. {}".format(idx+1, line[0]))
输出:
1. In bid to boost realty, state cuts stamp duty for 7 mths
2. India sees world’s third-biggest spike of 76,000+ cases, toll crosses 60k
3. Don’t hide behind RBI on loan interest waiver: SC to govt
上面发生的事情的细分:
[row for row in headlines if len(row[0]) > 1]
headlines
如果 的长度entry_in_headlines[0]
大于 1,这将创建一个新列表,其中包含所有条目。
sorted(<iterable>, key=lambda z: int(z[1]), reverse=True)
将使用 lambda 函数对给定的可迭代对象进行排序,该函数采用一个参数,并以整数形式返回该变量的第二个索引。然后反转结果,由于reverse=True
.
for idx, line in enumerate(<iterable>):
循环enumerate
将返回它被调用的次数的“计数”,以及迭代器内的下一个值。
print("{}. {}".format(idx+1, line[0]))
使用字符串格式化,我们在 for 循环内创建新字符串。
TA贡献1798条经验 获得超3个赞
我无法真正弄清楚您正在尝试什么或您的数据在哪里,但您需要添加一个 if 语句。
例如:
data = ['In bid to boost realty, state cuts stamp duty for 7 mths ', '42']
if len(data[0].split()) >= 2:
print(data[0])
任何 2 个字或更少的语句都不会被打印。
如果您有一个列表列表:
data = [['In bid to boost realty, state cuts stamp duty for 7 mths ', '42'],
['India sees world’s third-biggest spike of 76,000+ cases, toll crosses 60k',
'28'], ['O', '33'], ['Don’t hide behind RBI on loan interest waiver: SC to
govt ', '28']]
for lists in data:
if len(lists[0].split()) <= 2:
data.remove(lists)
print(*("".join(lists[0]) for lists in data), sep='\n')
添加回答
举报