我如何在数组概念中打印2个或更多值

时间:2019-05-18 11:50:18

标签: python arrays dart

我的代码是这样的:

main(){
var courselist = ["dart","flutter","swift","R"];
print(courselist[0]);
}

但是我想这样打印我的输出..'dart'和'R' 如何在数组概念中打印2个或更多值?

我试图这样做:

print(courselist[0],[3]);
print(courselist[0],courselist[3]);
print(courselist[0] & [3]);

我的代码:

main(){
var courselist = ["dart","flutter",24,0.45];
print(courselist[0]);
}

错误:

Harish@Harish:~/Desktop/dartfiles/Basics$ dart 01var_datatypes.dart 
01var_datatypes.dart:26:20: Error: The method '[]' isn't defined for the class 'Object'.
 - 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named '[]'.
print(courselist[2][0]);
                   ^^

3 个答案:

答案 0 :(得分:0)

您需要写courselist[i]来打印列表的第i个索引条目。您需要写print(courselist [0], courselist [3])来打印每个条目。写入print(courselist [0],[3])print(courselist [0]and[3])无效。如果要打印的索引很多,则可以通过遍历列表来避免多次写入:

vals=[0,3]
for val in vals:
    print(courselist [val])

您需要使用正确的PYTHON代码才能正常工作。

答案 1 :(得分:0)

如果要在添加标签时在fluttet(dart)中显示两个变量,可以按照以下说明进行操作。

print('Something ${variable1} ${variable 2} ');

您还可以在任何地方添加所需的任何文本。

答案 2 :(得分:0)

在带引号的字符串中,您可以通过评估表达式和变量来构建输出。 表达式用$ {expr}括起来。变量仅以$开头。例如。 $ varname,

main() {
  var courselist = ["dart", "flutter", "swift", "R"];
  print("Dart Course: ${courselist[0]},  R Course: ${courselist[3]}");

  num grade = 10;
  print("Amount $grade");
}