Python Forum
I want to simplify this python code into fewer lines, it's about string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to simplify this python code into fewer lines, it's about string
#1
Hi, I want to simplify this python code into fewer lines as possible, but I'm less experienced and I cannot do it:

code = [0, 1, 0, 1, 0, 1, 0, 1] # This is a separated line, which will be used as an argument in a function

def f(code, i):
    return ["a", "b", "c", "d", "e"][code[i]]
for i in range(len(code)):
    c += f(code, i)
print(c)
Output:
abababab
An example of simplification:
code = [0, 1, 0, 1, 0, 1, 0, 1] # This is a separated line, which will be used as an argument in a function

def f(code, i): # This is ok, in my real project, it's already simplified, this is just a draft.
    return ["a", "b", "c", "d", "e"][code[i]]
c = [x for x in ["a", "b", "a", "b", "a", "b", "a", "b"]] # this is wrong, it should using the function f, but I don't know how to do it?
print(c) # This shows list of characters, but not one string, so it's wrong.
Output:
['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
Reply
#2
Just needs switching around a little and make use of str join method, the function is not needed.
c = "".join(["a", "b", "c", "d", "e"][index] for index in code)
Reply
#3
maybe

code = [0, 1, 0, 1, 0, 1, 0, 1]
letters = ["a", "b", "c", "d", "e"]

def f():
    c = ''
    for i in range(len(code)):
        c += letters[code[i]]
    return c
    
print(f())
Reply
#4
My question for clearer, is how to turn a list of characters into one single string?
c = ['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
c = ... # What should be this line?
print(c)
It should print:
abababab

Edit: Solved
using join as one of you explained, bye.
Reply
#5
Use the str method join as stated in my post above.
c = ['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
c = "".join(c)
print(c)
Output:
abababab
mandaxyz likes this post
Reply
#6
Thank you so much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What are these python lines for? What are tey doing? Led_Zeppelin 7 1,629 Feb-13-2023, 03:08 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 1,697 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,626 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Editing text between two string from different lines Paqqno 1 1,322 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,232 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
Thumbs Up [SOLVED] Simplify condition test in if block? Winfried 2 1,716 Aug-25-2021, 09:42 PM
Last Post: Winfried
  python seems to be skipping lines of code alansandbucket 1 4,167 Jun-22-2021, 01:18 AM
Last Post: Larz60+
  reading lines from a string [Solved] ebolisa 14 6,410 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Running a few lines of code as soon as my timer ends nethatar 3 2,429 Feb-26-2021, 01:02 PM
Last Post: jefsummers
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,269 Feb-24-2021, 10:43 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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