我正在使用列表:
Main(chan_no,level,:,:) = Main(chan_no,level,:,:) + S_avg ;
我想做的是一方面处理字符串,另一方面处理子列表。
如果我写:
text = ['package1:', 'package2:', 'package1,', ['package2', 'package4'], 'package3:', ['package2', 'package1,'], ['package3', 'package10,'], 'package60']
输出如下
for i in range(len(text)):
print(type(text[i]))
if text[i] != str:
print(text[i],'Not String')
else:
print(text[i],'String')
哪个不正确,因为<class 'str'>
package1: Not String
<class 'str'>
package2: Not String
<class 'str'>
package1, Not String
<class 'list'>
['package2', 'package4'] Not String
<class 'str'>
package3: Not String
<class 'list'>
['package2', 'package1,'] Not String
<class 'list'>
['package3', 'package10,'] Not String
<class 'str'>
package60 Not String
实际上是一个字符串。
我的错误在哪里?
答案 0 :(得分:6)
print(type(text[i]))
if text[i] != str:
您打印type(text[i])
(类型),但正在将text[i]
(值)与类型进行比较。所以当然你最终得到的结果似乎毫无意义。只需将类型与str
进行比较即可。
话虽如此,检查类型的推荐方法是使用isinstance
:
if isinstance(text[i], str):
print(text[i], 'String')
else:
print(text[i], 'Not String')
答案 1 :(得分:0)
您可以像这样更改for循环中的if条件
// *** Set your Destination TimeZone, for Las Vegas its 'PDT/PST, change TimeZone accordingly for other conversions ***'
NSTimeZone* destinationTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"];
// *** Its your current system(device) time zone ***
NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
// *** Set your date to convert ***
NSDate *currentDate = [NSDate date];
// *** Calc time difference ***
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:currentDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
// *** set current real date ***
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
// *** Set Date Formater ***
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:destinationTimeZone];
[df setDateStyle:NSDateFormatterShortStyle];
[df setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"Converted Date %@",[df stringFromDate:date]);
我不确定但我觉得它对你有帮助。