I'm trying to insert something into a new list and get an output that says:
vehicle is 7 letters
fl=['vehicle','person']
Textfl=[]
Textfl.extend(str(fl[0])+ "is " +str(len(fl[0]))+ "letters")
print(Textfl)
Ends up returning:
['v', 'e', 'h', 'i', 'c', 'l', 'e', 'i', 's' ' ', '7', 'l', 'e', 't', 't', 'e', 'r', 's']
Wanted output: vehicle is 7 letters
You could use string formatting for a cleaner solution:
For Python 3.6+
s = 'vehicle'
output = f'{s} is {len(s)} letters'
# 'vehicle is 7 letters'
For older versions use:
output = '{} is {} letters'.format(s,len(s))
# 'vehicle is 7 letters'
The problem with your solution is that you are making the result a list, when you're expected output is actually a string. The following would be enough:
str(fl[0]) + " is " + str(len(fl[0]))+ " letters"
# 'vehicle is 7 letters'
fl = ['vehicle', 'person']
Textfl = str()
Textfl = fl[0]+ " is " + str(len(fl[0])) + " letters"
print(Textfl)
Try to use the Textfl object as string.
If you want a list in output containing an element vehicle is 7 letters then you can do something like this:
fl=['vehicle','person']
Textfl=[]
Textfl.append(str(fl[0])+" "+"is " +str(len(fl[0]))+" "+"letters")
print(Textfl)
output
['vehicle is 7 letters']
Related
I want to convert a string to a list.
fName = input("Enter your First Name: \n")
lName = input("Enter your Last Name: \n")
location = input("Enter your Location: \n")
string = fName + " " + lName + " " + location
print(string)
print("String coverted to list :",string.split())
# Output : String coverted to list : ['name', 'name', 'location']
print("String coverted to list :\n",list(string))
# Output: String coverted to list : ['n', 'a', 'm', 'e', ' ', 'n', 'a', 'm', 'e', 'l', 'o', 'c', 'a', 't', 'i', 'o', 'n' ]
Desired Result :
#Output : [ namenamelocation]
Basically I do not want the list to get separated after spaces and want the term string to be a single index in list.
# remove spaces and then put into list
[string.replace(" ", "")]
To add, if you don't add the spaces at the beginning, you don't need to use .replace(), just the brackets []
string = [fName + lName + location]
I need something like this
accepted_string: An apple a day keeps the doctor away
resultant_string: Aapedyepteotrwy
And for this I have written this code:
accepted_string = input("enter")
add = ''
for count in range(0, len(accepted_string), 2):
if accepted_string[count] == " ":
count += 1
add = add + accepted_string[count]
else:
add = add + accepted_string[count]
print(add)
But this is giving me different output while passing the above sample input. The logic is also correct, then where is the error? I have to skip to next character whenever I encounter a whitespace. Please help me as I am new to this
Simple string slicing with prior replacement:
>>> s = "An apple a day keeps the doctor away"
>>> s.replace(" ", "")[::2]
'Aapedyepteotrwy'
you can remove space before hands instead of doing in for loop and make it list to convert in into array
Code:
accepted_string = input("enter")
add = ''
accepted_string = list(accepted_string.replace(" ", ""))
print(accepted_string)
for count in range(0, len(accepted_string), 2):
add = add + accepted_string[count]
print(add)
output:
['A', 'n', 'a', 'p', 'p', 'l', 'e', 'a', 'd', 'a', 'y', 'k', 'e', 'e', 'p', 's', 't', 'h', 'e', 'd', 'o', 'c', 't', 'o', 'r', 'a', 'w', 'a', 'y']
Aapedyepteotrwy
2nd code:
accepted_string = input("enter")
add = ''
char_count = 1
for count in range(0, len(accepted_string)):
if accepted_string[count] != " ":
char_count += 1
if char_count % 2 == 0:
add = add + accepted_string[count]
# add = add + accepted_string[count]
# else:
# add = add + accepted_string[count]
print(add)
output:
Aapedyepteotrwy
I created a code that requires tabs to put in it, but I cannot seem to figure out how to add the tabs appropriately. See below for my code and the doc string for what it should return, and what it returns instead. Maybe I should rethink my whole approach?
def display_game(guesses, clues):
'''(list, list) -> str
Given two lists of single character strings, return a string
that displays the current state of the game
>>>display_game([['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']], [['b', 'b'], ['b','b', 'b', 'b']])
'Guess\tClues\n****************\nY P G G\tb b\nO O G G\tb b b b\n'
'''
display = 'Guess\tClues\n****************\n'
for i in range(len(guesses)):
for letter in guesses[i]:
display += letter + ' '
for letter in clues[i]:
display += letter + ' '
display += '\n'
return display
When I use it (using the doc string example), I get:
display_game([['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']], [['b', 'b'], ['b','b', 'b', 'b']])
'Guess\tClues\n****************\nY P G G b b \nO O G G b b b b \n'
Any attempt to put \t in the code has it turning out wrong (ex: with \t between each string instead of where they should be as per the doc string). Is anyone able to suggest how I may change things around? Thanks!
Your code does not add a tab in between the guess and the clue. You could simply add
display += '\t'
in between the first and second nested for loops, however, you then need to ensure that a trailing space is not added at the end of the first loop.
str.join() is a better way to handle this as it only adds delimiter strings in between the items of a sequence:
>>> ' '.join(['a', 'b', 'c'])
'a b c'
Notice that there is no trailing space character in the above. Applying that to your function:
def display_game(guesses, clues):
display = 'Guess\tClues\n****************\n'
for guess, clue in zip(guesses, clues):
display += '{}\t{}\n'.format(' '.join(guess), ' '.join(clue))
return display
zip() is also used here to pair each guess and clue. Then it's simply a matter of using str.join() on the guess and clue, and building the string with the tab in the required place.
>>> assert(display_game([['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']], [['b', 'b'], ['b','b', 'b', 'b']]) == 'Guess\tClues\n****************\nY P G G\tb b\nO O G G\tb b b b\n')
You can just add it in between the for loops:
for i in range(len(guesses)):
for letter in guesses[i]:
display += letter + ' '
display += '\t' # right here
for letter in clues[i]:
display += letter + ' '
display += '\n'
return display
This worked for me. Just add the tab between those two for loops of guesses and clues.
def display_game(guesses, clues):
display = 'Guess \t Clues \n **************** \n'
for i in range(len(guesses)):
for letter in guesses[i]:
display += letter + ' '
display += '\t'
for letter in clues[i]:
display += letter + ' '
display += '\n'
return display
print(display_game('at', 'yk'))
This gave output:
Guess Clues
****************
a y
t k
If i have lists for example:
['6'] #Number
['!'] #Punctuation
['r'] #Alphabet
['8'] #Number
['/'] #Punctuation
['e'] #Alphabet
['5'] #Number
[':'] #Punctuation
['l'] #Alphabet
I use data = line.strip().split(' ') to convert it into this form from a csv file.
I am trying to assign the elements in the lists to their respective variable
For example number will contain the lists that have numbers in it, punctuation will contain the lists that have punctuation in it and alphabet will have lists with alphabets.
What I can't understand is if I do something like
number = data[0], punc = data[1], alpha = data[2]
I get an error:
List index out of range.
So how can i solve this problem?
My code,
for line in new_file:
text = [line.strip() for line in line.split(' ')]
This part of your code appears to be fine
for line in new_file:
text = [line.strip() for line in line.split(' ')]
however if you are doing the following
for line in new_file:
text = [line.strip() for line in line.split(' ')]
number = text[0], punc = text[1], alpha = text[2]
You may ran into problems..take for example a line in your file below
"hello world"
if you split this line you will have a list like ["hello", "world"].This list contains two elements.
Now if you assign this result like text=["hello", "world"]
and you place this result in a variable like
alpha = text[2]
You will certainly recieve List index out of range. ..Why?
Because text[2] does not exist!
Some lines may contain less then 3 words (like in this example)
Revise your approach
Try using a dictionary approach
alpha={"alphabet":[]}
numb={"alphabet":[]}
punc={"punctuation":[]}
..iterate through the file and use list comprehension to select all punctuation, letters, etc and add it your the respective dictionary elements... If you are having trouble post your revised codes
EDIT A WORKING EXAMPLE HOW I WOULD TACKLE THIS
Let say I have a file named new_file and has the content below
hello my name is repzERO
AND THIS IS my age: 100 years
A python script I tried
import re
new_file=open("new_file","r")
alpha={"alphabet":[]}
numb={"number":[]}
punc={"punctuation":[]}
all_punctuation=""
for line in new_file:
alpha["alphabet"]+=[c for c in line if re.search("[a-zA-Z ]",c)]
numb["number"]+=[c for c in line if re.search("[0-9]",c)]
punc["punctuation"]+=[c for c in line if re.search("[^\w\s]",c)]
print(alpha)
print(numb)
print(punc)
output
{'alphabet': ['h', 'e', 'l', 'l', 'o', ' ', 'm', 'y', ' ', 'n', 'a', 'm', 'e', ' ', 'i', 's', ' ', 'r', 'e', 'p', 'z', 'E', 'R', 'O', 'A', 'N', 'D', ' ', 'T', 'H', 'I', 'S', ' ', 'I', 'S', ' ', 'm', 'y', ' ', 'a', 'g', 'e', ' ', ' ', 'y', 'e', 'a', 'r', 's']}
{'number': ['1', '0', '0']}
{'punctuation': [':']}
Your lists seems to have less elements.
Something like this:
yourVariableName = ["what", "ever", "elements", "are", "here"]
is called a list. The list above has 5 elements. You can access the elements with a numeric index i:
yourVariableName[i]
where i is in this case either 0, 1, 2, 3 or 4 (or negative number when you want to count from the end). When you try
yourVariableName[5]
or even higher, you get an "index out of range" error.
This is my current code:
key = input("Enter the key: ")
sent = input("Enter a sentence: ")
print()# for turnin
print()
print("With a key of:",key)
print("Original sentence:",sent)
print()
#split = sent.split()
blank = [ ]
for word in sent:
for ch in word:
blank = blank + ch.split()
print(blank)
print()
What i have now gives me a list of all the letters in my sentence, but no spaces. If i use this...
for word in sent:
for ch in word:
print(ch.split())
It gives me a list of all characters including the spaces. Is there to get this result and have it equal a variable?
If you just want a list of all characters in the sentence, use
chars = list(sent)
What you're doing is definitely not what you think you're doing.
for word in sent:
This doesn't loop over the words. This loops over the characters. This:
for word in sent.split()
would loop over the words.
for ch in word:
Since word is a character already, this loops over a single character. If it weren't for the fact that characters are represented as length-1 strings, this would throw some kind of error.
sent is of type string. and when you iterate over a string this way:
for word in sent:
you get the individual characters, not the words.
Then you iterate over a single char:
for ch in word:
and get that very same char (!).
And then with that split() call you convert a non-blank character, say 'x' into a list with itself as element (['x']) and a blank characters into the empty list.
You probably want something along the lines of:
for word in sent.split():
....
But if what you want is to build a list of words, no need to iterate, that's exactly what sent.split() will get you!
And if what you want is a list of chars, do list(sent).
From help(str.split):
split(...)
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
If you want individual characters of a string, pass it to list.
>>> list('This is a string.')
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '.']
I'm not 100% sure what you're asking, but it seems like....
blank = [ch for ch in sent]
...that's all you need....
Let me give you some sample Ins and Outs and see if that's what you want.
IN = "Hello world!"
OUT =>
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
Is that right?
string = "This here is a string"
>>> x = list(string) # I think list() is what you are looking for... It's not clear
>>> print x
['T', 'h', 'i', 's', ' ', 'h', 'e', 'r', 'e', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g']
>>> print string.split() # default arg is a space
['This', 'here', 'is', 'a', 'string']