简体   繁体   English

如何在列表中打印最多 n 个元素

[英]How to print upto n elements in a list

I want to print upto 50 elements in a list我想在列表中打印最多 50 个元素

ie If there are 100 elements in the list I need first 50 alone.即如果列表中有 100 个元素,我只需要前 50 个。 My current code for the above is我目前的上述代码是

'Filter':switcheroo.get(zaxis,["None Selected"])[zaxis].unique().tolist()[:50]

If there are 25 elements in the list I need 25 alone.如果列表中有 25 个元素,我只需要 25 个。 I get an error when the above code is used.使用上述代码时出现错误。 from other posts I understand that the below code is the solution, but I cant understand how to implement it with my current code.'从其他帖子我了解到以下代码是解决方案,但我无法理解如何使用我当前的代码实现它。

[x for _, x in zip(range(n), records)]

How to take the first N items from a generator or list in Python? 如何从 Python 中的生成器或列表中获取前 N 个项目?

first 50 elements of a list:列表的前 50 个元素:

print mylist[:50]

Test:测试:

newlist = [x for x in xrange(10)]
newlist2 = [x for x in xrange(100)]

print [x for x in newlist[:50]]
>[1,2,3,4,5,6,7,8,9]
print [x for x in newlist2[:50]]
>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
 #include <stdio.h>
 #include <string.h>
 struct arithmet {
 char  eval;
 int   first_num,sec_num;
  };

 /* function declaration */
 void eval_value( struct arithmet *valyu );
 int main( ) {

 struct arithmet valyu1;       
 struct arithmet valyu2;
 char oper;
 int num1,num2;
 gets(oper);
 scanf("%d",&num1);
 scanf("%d",&num1);
 valyu1.first_num=num1;
 valyu2.sec_num=num2;
 valyu1.eval=oper;
 eval_value(&num1);
 eval_value(&num2);
 eval_value(&per);

 return 0;
}
void eval_value(struct arithmet *valyu)
{
  valyu->first_num;
  valyu->sec_num;
  valyu->oper;
  if(oper=='+')
  {
    printf("%d",addit(int a,int b));
   }
  if(oper=='-')
  {
    printf("%d",subtractit(int a,int b));
   }
   if(oper=='*')
   {
        printf("%d",multiplyit(int a,int b));
   }
    if(oper=='/')
    {
        printf("%d",divideit(int a,int b));
    }
    if(oper=='@')
    {
       printf("%d",intdivideit(int a,int b));
    }
    if(oper=='%')
    {
        printf("%d",remdivideit(int a,int b));
    }
    if(oper=='~')
    {
        printf("%d",exponeit(int a,int b));
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM