V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
a476286557
V2EX  ›  Python

关于处理这样的数据......

  •  
  •   a476286557 · 2018-08-30 14:41:40 +08:00 · 2351 次点击
    这是一个创建于 2065 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有这样一个列表:list = [9,4,5,6,5,4,7,7,6]
    因为 9,4 是下降的,所以处理成 ret1 = [9,4]
    4,5,6 是上升的,所以处理为 ret2 = [4,6]
    6,5,4 是下降的,所以处理为 ret3 = [6,4]
    应该怎样处理,才能成为这样的呢?
    17 条回复    2018-08-31 09:13:21 +08:00
    xpresslink
        1
    xpresslink  
       2018-08-30 14:44:37 +08:00
    处理很容易,但是楼主的需求逻辑表述不清楚。
    a476286557
        2
    a476286557  
    OP
       2018-08-30 14:49:09 +08:00
    @xpresslink 不好意思,我再描述一下.
    像上述列表, 9,4 是属于下降趋势,所以把它存为[9,4],就是只存下降的起,始
    4,5,6 这一段属于上升趋势,存这段的起,始,就是存为[4,6] ..
    不知道这样表达是否可以
    ballshapesdsd
        3
    ballshapesdsd  
       2018-08-30 14:50:51 +08:00
    遍历一遍不就好了
    klesh
        4
    klesh  
       2018-08-30 14:59:36 +08:00   ❤️ 2
    拐点的特征很明显啊
    凸拐点: a[i - 1] < a[i] and a[i] > a[i + 1]
    凹拐点: a[i - 1] > a[i] and a[i] < a[i + 1]
    通过拐点特征, 配合边界值处理, 再用一个临时变量存上一个拐点, 就能整理出你想要的格式了.
    xpresslink
        5
    xpresslink  
       2018-08-30 15:01:29 +08:00
    @a476286557 那个 4 被前面 9 4 给消耗掉么? 7 7 怎么处理?
    a476286557
        6
    a476286557  
    OP
       2018-08-30 15:02:42 +08:00
    @xpresslink 不消耗,7 7 按照下降处理
    a476286557
        7
    a476286557  
    OP
       2018-08-30 15:03:05 +08:00
    @klesh 谢谢,我试试
    a476286557
        8
    a476286557  
    OP
       2018-08-30 15:03:56 +08:00
    @ballshapesdsd 遍历过程中不会处理..
    PureWhiteWu
        9
    PureWhiteWu  
       2018-08-30 15:26:28 +08:00
    怎么感觉是特别基础的算法题。。。找拐点的问题。。。。
    下次面试可以试试这题。
    a476286557
        10
    a476286557  
    OP
       2018-08-30 15:30:24 +08:00
    @PureWhiteWu 大佬,我是新手上路,帮帮忙...
    Yourshell
        11
    Yourshell  
       2018-08-30 15:54:34 +08:00 via iPhone
    一个循环判断 n 与 n+1 的大小保留为标识符
    a476286557
        12
    a476286557  
    OP
       2018-08-30 17:10:42 +08:00
    @klesh 谢谢大佬指点~
    xpresslink
        13
    xpresslink  
       2018-08-30 18:11:49 +08:00   ❤️ 1
    def check_sort(arg_list: list):
    □□□□if arg_list[-2] == arg_list[-1]:
    □□□□□□□□arg_list[-2] += 0.1
    □□□□if arg_list == sorted(arg_list, reverse=True):
    □□□□□□□□return 'DESC'
    □□□□if arg_list == sorted(arg_list):
    □□□□□□□□return 'ASC'
    □□□□else:
    □□□□□□□□return 'None'

    source_list = [9,4,5,6,5,4,7,7,6]

    result = {'ASC': [], 'DESC': []}

    length = len(source_list)
    temp_list = source_list[:2]

    i = 2
    while True:

    □□□□sort_status = check_sort( temp_list + [source_list[i]])

    □□□□if sort_status == 'None':
    □□□□□□□□result[check_sort(temp_list)].append([temp_list[0], temp_list[-1]])
    □□□□□□□□temp_list = [temp_list[-1], source_list[i]]
    □□□□else:
    □□□□□□□□temp_list.append(source_list[i])

    □□□□i += 1
    □□□□if length == i:
    □□□□□□□□result[check_sort(temp_list)].append([temp_list[0], temp_list[-1]])
    □□□□□□□□break

    print(result)

    {'ASC': [[4, 6], [4, 7]], 'DESC': [[9, 4], [6, 4], [7, 6]]}
    whoami9894
        14
    whoami9894  
       2018-08-30 18:32:53 +08:00 via Android   ❤️ 1
    ```python
    def sort(x):
    temp_high = 0
    temp_low = 0
    result = []
    for i in range(len(x)):
    if i == 0:
    continue
    if i == len(x) - 1:
    if x[i] <= x[i-1]:
    result.append((x[temp_high], x[i]))
    else:
    result.append((x[temp_low], x[i]))
    continue
    if x[i]>=x[i+1] and x[i]>x[i-1]:
    temp_high = i
    result.append((x[temp_low], x[i]))
    elif x[i]<=x[i-1] and x[i]<x[i+1]:
    temp_low = i
    result.append((x[temp_high], x[i]))
    else:
    continue
    return result
    a = [9,4,5,6,5,4,7,7,6]
    print(sort(a))

    ```
    ```
    [(9, 4), (4, 6), (6, 4), (4, 7), (7, 6)]
    ```
    whoami9894
        15
    whoami9894  
       2018-08-30 18:33:55 +08:00 via Android
    @whoami9894
    手机上的飘号好像有点问题
    a476286557
        16
    a476286557  
    OP
       2018-08-31 09:13:11 +08:00
    @xpresslink 谢谢!
    a476286557
        17
    a476286557  
    OP
       2018-08-31 09:13:21 +08:00
    @whoami9894 谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5503 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 06:47 · PVG 14:47 · LAX 23:47 · JFK 02:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.