我想在列表中打印最多50个元素
即 如果列表中有100个元素,我需要先单独50个。 我目前的代码是
'Filter':switcheroo.get(zaxis,["None Selected"])[zaxis].unique().tolist()[:50]
如果列表中有25个元素,我需要25个元素。 使用上面的代码时出错。 从其他帖子我了解下面的代码是解决方案,但我无法理解如何使用我当前的代码实现它。'
[x for _, x in zip(range(n), records)]
How to take the first N items from a generator or list in Python?
答案 0 :(得分:3)
列表的前50个元素:
print mylist[:50]
作为可迭代:
打印[x for m in mylist [:50]]
测试:
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]
答案 1 :(得分:0)
#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));
}
}