如果我在文本文件中的列表具有以下内容
我希望它打印出来像
然而,我当前的代码将其打印出来,如
我的代码是:
#import "UIImage+Resizing.h"
@implementation UIImage (Resizing)
-(UIImage*)resizedImageWithSize:(CGSize)size {
CGImageRef cgImage = [self CGImage];
size_t bitsPerComponent = CGImageGetBitsPerComponent(cgImage);
size_t bytesPerRow = CGImageGetBytesPerRow(cgImage);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(cgImage);
CGContextRef context = CGBitmapContextCreate(nil, size.width, size.height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), cgImage);
CGImageRef resizedImageRef = CGBitmapContextCreateImage(context);
UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef];
CFRelease(resizedImageRef);
CFRelease(context);
return resizedImage;
}
@end
如何切换名称最后两部分的顺序?我的代码在哪个区域错了,因为我不确定如何逆转它。如果我在反转函数之后添加一个print语句,它会将名称的每个部分分隔为一个字符串,但是当它加入时,它会以错误的顺序执行
答案 0 :(得分:2)
不要反转清单:
def addStudent(student):
reg,year,degree,*name= student.strip().split()
fullName = "%s, %s" % (name[-1], " ".join(name[:-1]))
return (reg, year, degree, name, fullName)
答案 1 :(得分:1)
或更改该行
fullName = name[0] + ", " + " ".join(name[1:])
到这个
fullName = name[0] + ", " + " ".join(name[-1:0:-1])
编辑:
dat = '1234567 1 C100 Bartholomew Homer Simpson'
def addStudent(student):
reg,year,degree,*name= student.strip().split(" ")
name = list(reversed(name))
# You have now::
# name = ['Simpson', 'Homer', 'Bartholomew']
# Therefore , name[-1:0:-1]
# will select from last item in list 'Bartholomew to
# the 0th one, the 0th being excluded in reverse
fullName = name[0] + ", " + " ".join(name[-1:0:-1])
return (reg,year,degree,name,fullName)
def printStud(studentTuple):
reg,year,degree,*name,fullName = studentTuple
reg = int(reg)
thisYear = "Year "+str(year)
print(format(fullName, "<32s")+format(reg,"<7d")+format(degree,">6s"),format(thisYear,">6s"))
输出:
In [1]:
Simpson, Bartholomew Homer 1234567 C100 Year 1
答案 2 :(得分:0)
如果您需要排序名称,可以使用sorted()
a=["metallica", "therion", "acdc"]
print a[0],", "+" ".join( sorted(a[1:]))
出:
metallica,acdc therion