I dont understand why my script is not iterating through all string.split elements?

2024/11/10 12:58:39

The objective of this python exercise is to build a function that turns text into pig latin, a simple text transformation that modifies each word by moving the first character to the end and appending "ay" to the end.

For example, python ends up as ythonpay.

I actually built this script, but I am confused as to why it is not iterating over all text.split elements? And why it is only modifying the last element?

def pig_latin(text):say = ""# Separate the text into wordswords = text.split()for word in words:# Create the pig latin word and add it to the listnew_word = word[1:] + word[0] + "ay"say =  "".join(new_word)# Turn the list back into a phrasereturn sayprint(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
Answer

This section here is why. You only have one new_word variable, so each time this loop runs, it overwrites the previous value. The only value that doesn't get overwritten is the last one, and you end up with a single string.

for word in words:new_word = word[1:] + word[0] + "ay"say =  "".join(new_word)

Instead, make sure that each new word ends up in a list. The most intuitive way to do it, IMO, is through list comprehension. Below is how you would format it for this, but look up how to do them. Seriously, it's a couple minutes of your time and they'll be one of your best friends as you continue to learn. You can also do the same thing with dictionaries.

pig_latin_text = [word[1:] + word[0] + "ay" for word in words]
say =  " ".join(pig_latin)
https://en.xdnf.cn/q/119728.html

Related Q&A

Unable to change the tick frequency on my chart

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes import numpy a…

Django Queryset foreign keys

I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in use…

How to reorder the columns of a CSV?

How can I re-order the columns of a CSV file using Python? These are the first rows of a CSV file I need to change:03;30269714;Ramiro Alberto;Nederz;active;pgc_gral 03;36185520;Andrea;Espare;active;pg…

Distance matrix in Python Pandas

I am a newbie in python, but I like to process data in pandas. I have a hundred pairs of CSV data such as passenger and bus stop data. The passenger structure data is Person, and XY coordinates (UTM-Me…

calculating catalan numbers using memoization

I am tring to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change?def catalan_mem(n, memo = None):if n==0:return 1if memo == None:memo = …

cron python file doesnt work on centos 7

im trying to schedule my pythohn script into Centos 7 with cron. On my script at start i have added this:#!/usr/local/bin/pythonand this is my cron file that i have create into folder that contain pyth…

Comma separated Matrix from txt files - continued

I need to form a matrix from a list of textfiles containing frequency distribution of expressions. Therefore, I created a list of all that text files (lof) from a directory and used it to build a matri…

Questions about training LLMs on large text datasets for text generation from scratch

I made a fully custom made GPT in Jax (with Keras 3), using Tensorflow for the data pipeline. Ive trained the model on the Shakespeare dataset and got good results (so no problem with the model). Now I…

Pandas - Update/Merge 2 Dataframes based on multiple matching column values

I have 2 dataframes left_df and right-df, which both have 20 columns with identical names and dtypes. right_df also has 2 additional columns with unique values on every row. I want to update rows in ri…

How do I fix scrapy Unsupported URL scheme error?

I collect url from command python and then insert it into start_urls from flask import Flask, jsonify, request import scrapy import subprocessclass ClassSpider(scrapy.Spider):name = mySpider#sta…