Python Forum
How to append at a specific position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append at a specific position
#1
This is a little hard to explain but if the code is ran things will start to make sense.

Right now when this program is ran it does this, when I enter a letter in the input that is part of line 1
['*', '*', '*', '*', '*', 'l']

My goal is to have it do this instead
['*', 'l', '*', '*', '*',]


line1 = 'alpha'
line2 = ['*', '*', '*', '*', '*',]
password = input('enter a letter in the password')

for i in line1:
    if i == password:
        line2.append(password)
        print(line2)
Does anyone know the solution?
Reply
#2
I would use enumerate and then index assignment.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-21-2019, 12:57 AM)ichabod801 Wrote: I would use enumerate and then index assignment.

Do you mean doing it this way? It did work if each letter is unique, however when there are two same letters within line 1 it will only detect and replace the first one and igonring the second one.
line1 = 'november'
line2 = ['*', '*', '*', '*', '*', '*', '*', '*',]
password = input('enter a letter in the password')

for i in line1:
    if i == password:
        line2[line1.index(i)] = password
        print(line2)
result of running this code:
['*', '*', '*', 'e', '*', '*', '*', '*']
Reply
#4
If I understand the objective correctly one can use zip and conditional expression:

>>> password = 'alpha'                                    
>>> display = '*' * len(password)                         
>>> guess = 'h'                                           
>>> display = ''.join(char if char == guess else star for char, star in zip(password, display))                 
>>> display                                               
>>> '***h*'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Sep-21-2019, 04:14 AM)perfringo Wrote: If I understand the objective correctly one can use zip and conditional expression:

>>> password = 'alpha'                                    
>>> display = '*' * len(password)                         
>>> guess = 'h'                                           
>>> display = ''.join(char if char == guess else star for char, star in zip(password, display))                 
>>> display                                               
>>> '***h*'

Thanks, I think that just solved the problem, do you have a link to a site or document that explains how this works?
Reply
#6
Documentation from python.org:

built-in function zip(),
string method str.join(),
conditional expressions,
generator expression

Python is so concise that expressing it in spoken language is much longer and complicated:

(char if char == guess else star for char, star in zip(password, display))

"generate character if character is equal to guess else generate star for every character - star pair in zipped password and display",

''.join

"join generated chars and stars into string"

display =

Assign value of newly generated string to display (required if one want to repeat the operation)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to append a value to specific excel cell using openpyxl hobbyist 0 4,761 Mar-05-2021, 07:14 PM
Last Post: hobbyist
  Printing string at specific position on terminal - not showing __Mathieu__ 1 2,331 Sep-07-2020, 10:32 AM
Last Post: Larz60+
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,523 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  Delete specific lines contain specific words mannyi 2 4,064 Nov-04-2019, 04:50 PM
Last Post: mannyi
  [split] How to write at a very specific position of a text file Lalit 1 4,015 Feb-01-2018, 04:44 PM
Last Post: dalwood

Forum Jump:

User Panel Messages

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