在Flutter中创建一个非常简单的可滚动列表时,说(where widgets is List<Widget> == true)有什么优点和缺点:
选项1:
var widget = new SingleChildScrollView(
child: new Column(
chidren: widgets
));
Run Code Online (Sandbox Code Playgroud)
选项2:
var widget = new ListView(children: widgets);
Run Code Online (Sandbox Code Playgroud) 我已经确认了两个长度相同的清单。我想同时操作它们。我的代码当前看起来像这样:
var a = [1, 2, 3];
var b = [4, 5, 6];
var c = [];
for(int i = 0; i < a.length; i++) {
c.add(new Foo(a, b));
}
Run Code Online (Sandbox Code Playgroud)
以这种方式计算列表A的长度似乎有些笨拙。有没有更好,更Dart-y的方式来完成此任务?
其他问题表明,Python f 字符串在某个小数点处对数字进行四舍五入,而不是截断它们。
我可以用下面的例子来展示这一点:
>>> number = 3.1415926
>>> print(f"The number rounded to two decimal places is {number:.2f}")
The number rounded to two decimal places is 3.14
>>>
>>> number2 = 3.14515926
>>> print(f"The number rounded to two decimal places is {number2:.2f}")
The number rounded to two decimal places is 3.15
Run Code Online (Sandbox Code Playgroud)
然而,我遇到了另一种情况,Python 似乎截断了数字:
>>> x = 0.25
>>> f'{x:.1f}'
'0.2'
Run Code Online (Sandbox Code Playgroud)
我本以为0.25,精确到小数点后一位,会四舍五入为0.3。我是否遗漏了什么,或者这种行为不一致?
StatelessWidgets的所有代码,甚至是build方法,都在一个类中。对于StatefulWidgets,为什么将Statea类与Widget?分开?难道它们不能合并在一起,而StatefulWidget只能调用setState自身吗?
我有一本看起来像这样的字典:
letters_by_number = {
1: ['a', 'b', 'c', 'd'],
2: ['b', 'd'],
3: ['a', 'c'],
4: ['a', 'd'],
5: ['b', 'c']
}
Run Code Online (Sandbox Code Playgroud)
我想将其反转为如下所示:
numbers_by_letter = {
'a': [1, 3, 4],
'b': [1, 2, 5],
'c': [1, 3, 5],
'd': [1, 2, 4]
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过遍历(键,值)letters_by_number,遍历value(这是一个列表)并将(val,key)添加到字典中的列表来做到这一点。这很麻烦,我觉得必须有一种更“ pythonic”的方式来做到这一点。有什么建议么?