本文将通过for ... in ...的语法结构,遍历字符串.列表.元组.字典等数据结构. 字符串遍历 >>> a_str = "hello itcast" >>> for char in a_str: ... print(char,end=' ') ... h e l l o i t c a s t 列表遍历 >>> a_list = [1, 2, 3, 4, 5] >>> for num in a_list:
对于enum类型: 使用foreach遍历enum类型的元素并填充combox foreach ( HatchStyle hs1 in Enum.GetValues(typeof(HatchStyle))) { comboBox1.Items.Add(hs1.ToString()); } From a string: YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString); From an int: YourEnum
我们已经了解了Python支持布尔类型的数据,布尔类型只有True和False两种值,但是布尔类型有以下几种运算:与运算:只有两个布尔值都为 True 时,计算结果才为 True.True and True # ==> TrueTrue and False # ==> FalseFalse and True # ==> FalseFalse and False # ==> False或运算:只要有一个布尔值为 True,计算结果就是 True.True or Tru
List接口的toArray方法可以把一个结合转化为数组,但是使用不方便,toArray()方法返回的是一个Object数组,所以需要自行转变. toArray(T[] a)虽然返回的是T类型的数组,但是还是需要传入一个T类型的数组,这也挺麻烦的.我们期望输入的是一个泛型化的list,这样就能转化为泛型数组了. 看代码: import java.util.Arrays; import java.util.List; public class Client<T> { public static
python遍历一个目录,输出所有文件名 python os模块 os import os def GetFileList(dir, fileList): newDir = dir if os.path.isfile(dir): fileList.append(dir.encode('gbk')) elif os.path.isdir(dir): for s in os.listdir(dir): #如果需要忽略某些文件夹,使用以下代码 #if s == "xxx":