如何在python循环中排除一些索引?

时间:2015-06-29 21:27:13

标签: python django

我有这个列表清单:

"data" : [ 
[ 1, "EEBC92F4-DA8C-4B58-8730-3119F6B1C045", 1, 1386882230, "399231", 1386882230, "399231", "{\n}", "2010", "NON-HISPANIC BLACK", "MALE", "HUMAN IMMUNODEFICIENCY VIRUS DISEASE", "297", "5" ], 
[ 2, "84C91A4A-19E2-4AD2-9493-17B84707CA4E", 2, 1386882230, "399231", 1386882230, "399231", "{\n}", "2010", "NON-HISPANIC BLACK", "MALE", "INFLUENZA AND PNEUMONIA", "201", "3" ]]

我试图让他们这样:

for a in list
    for b in a
        print b

但当然,我得到了一切。

我只需要年度(2010年,2011年)和之前的项目。

我怎样才能得到它们?

3 个答案:

答案 0 :(得分:1)

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(basePackages = {"com.dal.dao.security", "com.services.security"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    LocalUserDao userDao;

    @Autowired
    UserRoleMapper roleMapper;

    @Autowired
    StandardPasswordEncoder passwordEncoder;

    private UserDetailsService methodSecurityService() throws Exception {
        return new UserDetailsServiceImpl(userDao, roleMapper);
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(methodSecurityService()).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .antMatchers("/*")
                .authenticated()

                .and()

                .formLogin()
                .loginPage("/login.htm").failureUrl("/login.htm?error")
                .usernameParameter("username")
                .passwordParameter("password")
                .and().logout().logoutSuccessUrl("/index.htm")
                .and().csrf()
                .and().exceptionHandling().accessDeniedPage("/403");
    }

}

如果您只想打印,可以这样做:

l = [ [ 1, "EEBC92F4-DA8C-4B58-8730-3119F6B1C045", 1, 1386882230, "399231", 1386882230, "399231", "{\n}", "2010", "NON-HISPANIC BLACK", "MALE", "HUMAN IMMUNODEFICIENCY VIRUS DISEASE", "297", "5" ],
    [ 2, "84C91A4A-19E2-4AD2-9493-17B84707CA4E", 2, 1386882230, "399231", 1386882230, "399231", "{\n}", "2012", "NON-HISPANIC BLACK", "MALE", "INFLUENZA AND PNEUMONIA", "2011", "3" ]]

#get index of the year
l[0].index('2010') #8
l[1].index('2012') #8

,它提供以下输出:

for x in l:
    for el in x[8:]:
        print el

不完全确定这是否是您想要的,但您写道,您需要在相应的年份之后获得所有信息("我在每一行之后都提到所有信息。" ;,你在评论中对James的回复)所以你可以先提取这些元素并将它们存储在一个新的列表中(如果你想用它们做其他事情除了打印):

2010
NON-HISPANIC BLACK
MALE
HUMAN IMMUNODEFICIENCY VIRUS DISEASE
297
5
2012
NON-HISPANIC BLACK
MALE
INFLUENZA AND PNEUMONIA
2011
3

lmod = [x[8:] for x in l] 然后看起来像这样:

lmod

现在可以按照您的方式打印:

[['2010',
  'NON-HISPANIC BLACK',
  'MALE',
  'HUMAN IMMUNODEFICIENCY VIRUS DISEASE',
  '297',
  '5'],
 ['2012',
  'NON-HISPANIC BLACK',
  'MALE',
  'INFLUENZA AND PNEUMONIA',
  '2011',
  '3']]

输出:

for sl in lmod:
   for el in sl:
       print el

那是你在找什么?

答案 1 :(得分:0)

for a in mylist:
    if int(a[8]) > 2009:
        for b in a:
            print b,
        print

这将遍历mylist中的每个项目(从掩盖内置的list重命名),检查索引8处的元素是否大于2009,然后打印出项目&# 39; s元素由单个空格分隔。如果您想要更多地控制输出,请使用字符串格式。

答案 2 :(得分:-1)

你并没有给出一个很好的例子,但你可以使用一个生成器:

filteredList = (x for x in list if int(x[8]) >= 2010)
for a in filteredList:
    for b in a:
        print b

或者,你可以直接写它:

for a in (x for x in list if int(x[8]) >= 2010):
    for b in a:
        print b

但是,您可能希望此处的“记录”为dict而不是列表。