Python 查找元素并记录问题,恳请给个思路。 - V2EX
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 1962 次点击
    这是一个创建于 1643 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 /td>
        10
    hellogitooxx  
    OP
       2021-04-22 22:17:11 +08:00
    @hello2060 我找到思路了,感谢你的回答。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5074 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 03:58 PVG 11:58 LAX 20:58 JFK 23:58
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86