用双引号加入单词

时间:2017-12-14 12:34:33

标签: python-3.x

以下是清单:

a = ['a', 'b', 'c']

我想将列表转换为字符串,如下所示:

string = '"a", "b", "c"'

我尝试过加入,但不能。代码是单词列表:

for i in codes:
    a = '`%s`, '.join(%code for code in codes)

2 个答案:

答案 0 :(得分:0)

简单的馅饼:

string = ""
for i in a:
    string = string + "\"" + i + "\", "
string = string[:-2:]

按照惯例一个接一个地加入它们,然后在最后删除额外的逗号和空格。

答案 1 :(得分:0)

只需使用#import "RecipesViewController.h" #import "DemoNavigationController.h" #import "NewSideViewController.h" #import "RecipesCategoriesCollectionViewCell.h" #import "RecipesTableViewCell.h" #import "RecipesHorizontalCell.h" #import "VerticalCollectionViewCell.h" #import "MainCollectionViewCell.h" @interface RecipesViewController () <UICollectionViewDataSource,UICollectionViewDelegate> { NSMutableArray *recipeTypeArray; RecipesHorizontalCell *cell; VerticalCollectionViewCell *verticalCell; MainCollectionViewCell *mainCell; } @property (strong, nonatomic) NSMutableArray *images; @end @implementation RecipesViewController - (void)viewDidLoad { [super viewDidLoad]; [self setUpInitialView]; } -(void)setUpInitialView{ recipeTypeArray = [[NSMutableArray alloc]initWithObjects:@"ALL",@"PROTEIN VEGGIE",@"FRUIT",@"FAST FUEL",@"CARB",@"VEGETERIAN",@"SEA FOOD",@"CHICKEN", nil]; _images = [@[@"bg-sidebarProfile.jpg"]mutableCopy]; [self.mainCollectionView registerNib:[UINib nibWithNibName:@"MainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MainCollectionViewCell"]; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return recipeTypeArray.count; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ if(collectionView == _horizontalCollectionView){ cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; [cell.recipeTypeBtn setTitle:[recipeTypeArray objectAtIndex:indexPath.row] forState:UIControlStateNormal]; return cell; } else { verticalCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCell" forIndexPath:indexPath]; verticalCell.commentTextView.layer.borderColor = [UIColor blackColor].CGColor; verticalCell.commentTextView.layer.borderWidth = 0.5; _verticalCollectionView.pagingEnabled = true; return verticalCell; } // else{ // if(mainCell==nil){ // mainCell= [_mainCollectionView dequeueReusableCellWithReuseIdentifier:@"MainCollectionViewCell" forIndexPath:indexPath]; // } // return mainCell; // } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if(collectionView ==_verticalCollectionView){ return collectionView.bounds.size; } else{ return CGSizeMake(150, 120); } } @end str.join()函数:

str.format()

输出:

a = ['a', 'b', 'c']
res = ', '.join('"{}"'.format(i) for i in a)
print(res)