Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is my string empty?
#1
Hi,

I want to create a loop that would attach the ids from a certain dictionary to a string, space between ids.

import string

letters = list(string.ascii_lowercase)

dic = {}
lista = []
strin=()
final=[]
for id, i in enumerate(letters):
    dic[id]=i

def alphabet_position(text):
    for i in text:
           strin=' '.join([str(k+1)+v for k,v in dic.items() if v == i.lower()])
But the 'strin' returned is empty. It works fine when I remove the condition in the list comprehension (if v==i.lower())).

Why? Thank you
Reply
#2
In the first place, why are you building a dictionary that maps from position to letter, but then using it to look from letter to position? My first thought is that you should build the dictionary in that direction and use it naturally. What this program does is use the dictionary like a list. It would be simpler to just loop through the original string.ascii_lowercase than loop through the dictionary.

Next, while alphabet_position loops over the characters in the input, it overwrites strin each time through the loop. So only the final character is kept in that variable.

Finally, the function has no return call. So if you are calling it, you will always get back None
Reply
#3
(Jan-30-2021, 02:24 AM)bowlofred Wrote: In the first place, why are you building a dictionary that maps from position to letter, but then using it to look from letter to position? My first thought is that you should build the dictionary in that direction and use it naturally. What this program does is use the dictionary like a list. It would be simpler to just loop through the original string.ascii_lowercase than loop through the dictionary.

Next, while alphabet_position loops over the characters in the input, it overwrites strin each time through the loop. So only the final character is kept in that variable.

Finally, the function has no return call. So if you are calling it, you will always get back None

Thanks for the reply. Your first point is great and makes sense.

For your latter part of the comment, why does it overwrite? When I add the ids to some list, the list is not being overwritten.
Here's the code with adding to the list. I also added returns.

def alphabet_position(text):
    for i in text:
        lista.append([k+1 for k,v in dic.items() if v == i.lower()])
        strin=' '.join([str(k+1)+v for k,v in dic.items() if v == i.lower()])
Reply
#4
Strings are immutable. They cannot be modified.
karabakh likes this post
Reply
#5
(Jan-30-2021, 02:29 AM)karabakh Wrote: When I add the ids to some list, the list is not being overwritten.

That's because you're using a method of the list that modifies it, like list.append(). What you're not doing is reassigning the list each time through like list = [new_item].

With strings, you'd instead assign the existing part and the new part to a variable. Something like:

full_string = ""
for s in list:
    full_string += s  # Keeps the existing portion each time through the loop.
karabakh likes this post
Reply
#6
(Jan-30-2021, 03:52 AM)bowlofred Wrote:
(Jan-30-2021, 02:29 AM)karabakh Wrote: When I add the ids to some list, the list is not being overwritten.

That's because you're using a method of the list that modifies it, like list.append(). What you're not doing is reassigning the list each time through like list = [new_item].

With strings, you'd instead assign the existing part and the new part to a variable. Something like:

full_string = ""
for s in list:
    full_string += s  # Keeps the existing portion each time through the loop.

Thank you. I also discovered extend which seems to be what you are mentioning here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  -i option changes sys.path (removes leading empty string '') markanth 6 1,898 Aug-26-2022, 09:27 PM
Last Post: markanth
  build a list (add_animals) using a while loop, stop adding when an empty string is en nikhilkumar 1 8,842 Jul-17-2017, 03:29 PM
Last Post: buran
  if line edit is empty put in a string birdieman 5 4,888 Mar-16-2017, 01:52 PM
Last Post: birdieman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020