简体   繁体   English

如何在列表中的正数之后打印第一个负数?

[英]How can I print first negative number after a positive number in a list?

I want to print first negative number after a positive number in a python list.我想在 python 列表中的正数之后打印第一个负数。 But I am unable to do this但我无法做到这一点

lst = [2,4,-4,-5,-7,2,3,5,6,-9,-4,-6,3,45,6,-67,-45,-56]

In this list I want to print only -4,-9,-67在这个列表中我只想打印 -4,-9,-67

Try:尝试:

lst = [2, 4, -4, -5, -7, 2, 3, 5, 6, -9, -4, -6, 3, 45, 6, -67, -45, -56]

out = [b for a, b in zip(lst, lst[1:]) if a > 0 and b < 0]
print(out)

Prints:印刷:

[-4, -9, -67]

With Python 3.10+:使用 Python 3.10+:

from itertools import pairwise

for a, b in pairwise(lst):
    if a > 0 > b:
        print(b)

Without:没有:

a = 0
for b in lst:
    if a > 0 > b:
        print(b)
    a = b

you could do something like this:你可以这样做:

for c, item in enumerate(lst): # go through all of the items and their indexs
    if item < 0 and c > 0 and lst[c - 1] >= 0: # check if the previous item is positive and the current number is negative
        print(item) # print the current item

A little bit modified answer of @Andrej Kesely, without producing sliced list (by means of using indexes instead) you can achieve same result @Andrej Kesely 的一点修改答案,不产生切片列表(通过使用索引代替)你可以获得相同的结果

out = [lst[i + 1] for i in range(len(lst) - 1) if lst[i] > 0 and lst[i + 1] < 0]

# [-4, -9, -67]

NOTE: You haven't specified what should be the right approach to a 0 in the list, that could slightly change the answer.注意:您尚未指定列表中0的正确方法,这可能会稍微改变答案。

The cleanest way is probably:最干净的方法可能是:

[b for a,b in zip(lst, lst[1:]) if a > 0 > b]

(But it's not very efficient for large lists, because it copies the list) (但是对于大型列表来说效率不是很高,因为它会复制列表)

A more efficient way is probably:一种更有效的方法可能是:

[lst[i] for i in range(1, len(lst)) if lst[i - 1] > 0 > lst[i]]

(But it's less elegant) (但它不那么优雅)

You could always use iterators (...) instead of list comprehensions [...] if you need more memory efficiency as well.如果您还需要更高的内存效率,则始终可以使用迭代器(...)而不是列表推导[...]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用异常处理在python中打印指定索引中的数字是正数还是负数? - How can I print whether the number in the specified index is positive or negative in python using exception handling? 如何检查列表中的整数或浮点数是正号还是负号? - How can I check if an integer or float number in a list is positive or negative in sign? 如何将负数转换为正数? - How to convert a negative number to positive? 如何获得正数的负二进制数 - How to get the negative binary number of a positive number 如何将正负十进制数列表规范化到特定范围 - How to normalize a list of positive and negative decimal number to a specific range python正数和负数列表的可能性 - python positive and negative number list possibilities 如果条形编号为负数而正数条形为另一种颜色,我如何为图表设置条件格式? xlxswriter - how can i have conditional formatting for a graph if the bar number is negative and another colour for positive bar? xlxswriter 如何正确分类图像中正(亮)圈和负(暗)圈的数量 - How can I correctly classify the number of positive (bright color) circles and negative (dark color) circles in the image 如何通过使用python在excel中查找指定数据(正数首先出现而负数在附近) - How to find specified data in excel by using python (positive number first appeared and negative number nearby) 如果数字是正数还是负数(包括0),如何返回1或-1? - How to return 1 or -1 if number is positive or negative (including 0)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM