Home > Back-end >  Print List elements in loop without Brackets and Quote marks
Print List elements in loop without Brackets and Quote marks

Time:03-25

I am trying to print the elements of a list inside a loop but the elements are displaying surrounded by ['']. Is there any way to format output to remove these. Further code and examples shown below

This is the code i am running:

        for i in range(len(get_recipe_titles)): 
            if i ==0:
                j = get_recipe_titles[i]; 
                Title = recipedf[recipedf['id'] == j] 
                Title = Title['title'].values 
                print('Recipe Recommendations for: {0}'.format(Title[0]))
            else: 
                j = get_recipe_titles[i];
                Title = recipedf[recipedf['id'] == j]
                Title = Title['title'].values
                Distance =dist.flatten()[i]
                i = i 1
                print(Title, "With Distance of ", Distance)`

The output is this. I want the [''] removed from the titles of the recepies:

Recipe Recommendations for: Three-tier red velvet cake

['Daffodil biscuits'] With Distance of 2.23606797749979

['Belgian waffles'] With Distance of 2.449489742783178

['Easy brownies'] With Distance of 2.6457513110645907

['Red velvet cake'] With Distance of 2.6457513110645907

["Cheat's waffles"] With Distance of 2.6457513110645907

['Scary Halloween cookies'] With Distance of 2.6457513110645907

['Chocolate chip muffins'] With Distance of 2.8284271247461903

['Bundt cake'] With Distance of 2.8284271247461903

['Chocolate beetroot cakes'] With Distance of 2.8284271247461903

I have used .format() in the code above to print the title on the first line of the output without Brackets and Quotes but cant seem to use it for the rest of the output. Is there any way to remove them or is there anywhere i am going wrong

CodePudding user response:

You probably just need to access the zero'th element of the numpy array Title like you did in the first part of the code.

See you did this in the first part:

print('Recipe Recommendations for: {0}'.format(Title[0]))

so do this in the second part:

print(Title[0], "With Distance of ", Distance)

CodePudding user response:

Title['title'].valuesseems to be a list of strings. If you want to only the first string inside you could use `Title['title'].values[0]. It depends on your data structure, but if there can be more or less titles than one you should use this:

", ".join(Title['title'].values)

That will join all strings in Title['title'].values with an , .

  • Related