The list signals_by_date stores tuples and each tuple contains 15 numbers. For each tuple within signals_by_date, I want to remove numbers that don't satisfy certain criteria. For some reason, no matter what constraints I put in the list comprehension, I'm always left with 7 numbers in each tuple. In the code example below, all numbers are less than 3, so I would expect each tuple to be empty. What am I doing wrong? Many thanks.
signals_by_date = []
for i in range(0, 1):
temp_signals = []
for symbol in symbols:
for signal in signals_by_symbol[symbol]:
temp_signals.append(signal[i]-1)
signals_by_date.append(temp_signals)
[signals_by_date[i].remove(v) for v in signals_by_date[i] if v < 3]
最后一行应为:
signals_by_date[i] = [v for v in signals_by_date[i] if v >= 3]
Presumably, as you remove each item you're shifting the items in the list such that you skip seeing every second one.
In general, I believe list comprehensions are intended to accrue new lists from existing ones rather than to produce side effects on the items in the original list; and certainly not to change the structure of the comprehended list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.