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
hellogitooxx
V2EX  ›  Python

Python 查找元素并记录问题,恳请给个思路。

  •  
  •   hellogitooxx · 2021-04-22 18:18:27 +08:00 · 1454 次点击
    这是一个创建于 1097 天前的主题,其中的信息可能已经有所发展或是发生改变。
    if_list=["oo","不存在"]
    list_data=["aab","abb","bba","acc","adcd","bao","ccd"] 
    str_="aa"
    
    for a_list in list_data:
         if str_[-2:] == a_list[:2]:
         	if_list.append(a_list[-1])
         else:
         	if_list.update("不存在")
    

    我想循环 list_data 列表,当 str_="aa"有和列表中元素前面两个字符相同的,则返回对应元素最后一个,没有相同的,也要返回“不存在”,但是我这个程序不相同的,返回了“不存在”多个了,有什么好的设计呢? 恳请给个思路。

    10 条回复    2021-04-22 22:17:11 +08:00
    wlsnx
        1
    wlsnx  
       2021-04-22 18:26:08 +08:00   ❤️ 1
    list_data[-1] if any(str_[-2:] == a[:2] for a in list_data) else "不存在"
    hello2060
        2
    hello2060  
       2021-04-22 18:26:31 +08:00 via iPhone   ❤️ 1
    你这是找错误吧?不用 else 在函数返回前检查一下 if_list, 为空的话插入一个不存在啊。

    不过说实话你这到底要返回啥似乎都没说清楚
    Wincer
        3
    Wincer  
       2021-04-22 18:27:42 +08:00   ❤️ 1
    首先:比较前两个字符相同可以使用 a_list.startswith(str_),其次你这里 if_list 又是 append 操作又是 update 操作,如果想里面不包含重复的,就把 if_list 设置成 set(),或者设置一个标志位默认为 0,当字符串相同的时候标志位置 1,else 操作就不需要了,for 循环之后判断这个标志位如果还是 0,就添加 “不存在” 进 if_list
    hellogitooxx
        4
    hellogitooxx  
    OP
       2021-04-22 19:16:19 +08:00
    @Wincer
    else:
    if_list.update("不存在")这里写错了,应该是 if_list.append("不存在")进去的。想要得到的结果就是 for a_list in list_data: 当相同的时候,要返回“不存在”,只需要返回一次就行了,但是我上面这个程序不相同的,返回了“不存在”多个了了。
    hellogitooxx
        5
    hellogitooxx  
    OP
       2021-04-22 19:22:51 +08:00
    @hello2060
    ```

    for a_list in list_data:
    if str_[-2:] == a_list[:2]:
    if_list.append(a_list[-1])
    else:
    if_list.update("不存在")
    ```
    没有相同的,也要返回“不存在”,但是我这个程序不相同的,返回了“不存在”多个了,我只需要返回一个”不存在“
    hellogitooxx
        6
    hellogitooxx  
    OP
       2021-04-22 19:25:21 +08:00
    @Wincer if_list 里面是要允许重复值的,这是前提,我这个题目主要是,当 str_="aa"有和列表中元素前面两个字符相同的,刚把当前元素添加到 if_list 中,要是没有就添加”不存在“。
    hello2060
        7
    hello2060  
       2021-04-22 19:29:32 +08:00
    ```
    for a_list in list_data:
    if str_[-2:] == a_list[:2]:
    if_list.append(a_list[-1])

    return len(if_list) == 0 ? if_list : ['不存在']
    ```
    hellogitooxx
        8
    hellogitooxx  
    OP
       2021-04-22 19:42:27 +08:00
    @hello2060

    是需要在 if_list 前提上.append 的,return len(if_list) == 0 ? if_list : ['不存在'] 这个,i 前面 f_list=["oo","不存在"]已经有两个元素了。
    hello2060
        9
    hello2060  
       2021-04-22 20:37:17 +08:00 via iPhone   ❤️ 1
    @hellogitooxx 说实话,我估计人没看的懂你到底想做啥。。
    hellogitooxx
        10
    hellogitooxx  
    OP
       2021-04-22 22:17:11 +08:00
    @hello2060 我找到思路了,感谢你的回答。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2535 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:06 · PVG 00:06 · LAX 09:06 · JFK 12:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.