我想检查嵌套列表中是否存在某些内容

时间:2015-10-22 23:07:25

标签: list python-3.x if-statement while-loop nested

我希望你能帮忙看一下代码

@Configuration
public class JpaConfiguration {

@Bean()
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
    LocalContainerEntityManagerFactoryBean emFactory = new LocalContainerEntityManagerFactoryBean();
    emFactory.setPersistenceProviderClass(FilterEnableHibernatePersistenceProvider.class);
    //yours configuration
}

我想要它,以便如果指定的人已经在列表中,它将他们的新统计数据添加到3的列表并推出最旧的一个(在while循环中得到的那个),但我想添加该人到嵌套列表的末尾,如果它们还没有出现(新条目),请看一下#this是我的头脑中的部分!

为时已晚,我喝了过多杜松子酒吗?

请帮助我的大脑疼痛

罗斯

2 个答案:

答案 0 :(得分:0)

#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
    sheros = [['Groot',23,34,35],['Rocket',45,56,67]]

    print(sheros)
    newname = input('what is your name? ' )
    newstat = int(input('what is the new stat? ' ))

    found = False  # Use a flag to track whether name was found
    for entry in sheros:  # Don't use counter. Just loop over elements.
        if entry[0] == newname:
            entry[2:4] = entry[1:3]  # Use list slicing
            entry[1] = newstat
            found = True
            break  # Exit early assuming names are unique.

    if found:
        print("Already here")
    else:
        sheros.append([newname, newstat, 0, 0])

    print(sheros)


if __name__ == "__main__":
    main()

答案 1 :(得分:0)

制作这样的功能。它可以帮到你。

def is_elem(l, e):
    return any(e==i[0] for i in l)

实施例

In [2]: sheros=[['Groot',23,34,35],['Rocket',45,56,67]]

In [3]: is_elem(sheros, 'Groot')
Out[3]: True

In [4]: is_elem(sheros, 'Other')
Out[4]: False