Python Forum
Sublist/ Subarray into string Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sublist/ Subarray into string Python
#1
Question 
I have a little problem , I have this code and I need to convert the final print to a string, to be able to printthe final results in different lines and change the separator "," to a "+". If someone could help me to fix this I will be grateful :D.

Ex.: Input: 7

Output: 5 + 2
5 + 1 + 1
2 + 2 + 2 + 1
2 + 2 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1
Thks in advance ^^.

def change(coins, amount):
    res = []

    def getchange(end, remain, cur_result):
        if end < 0: return
        if remain == 0:
            res.append(cur_result)
            return
        if remain >= coins[end]:
            getchange(end, remain - coins[end], cur_result + [coins[end]])
        getchange(end - 1, remain, cur_result)

    getchange(len(coins) - 1, amount, [])
    return res


q = int(input("Write your change: "))
st = change([1, 2, 5, 10], q)
print(st)
Reply
#2
Something like this?:

>>> list_ = [5, 1, 1]
>>> print(' + '.join([str(i) for i in list_]))
5 + 1 + 1
SantiagoPB likes this post
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
#3
Wow it function perfectly, thank you a lot!

(Apr-23-2021, 06:44 PM)perfringo Wrote: Something like this?:

>>> list_ = [5, 1, 1]
>>> print(' + '.join([str(i) for i in list_]))
5 + 1 + 1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List of objects with sublist medatib531 4 2,315 Mar-01-2020, 06:16 PM
Last Post: buran
  Insert into sublist if sublist is not having same no of records. parthi1705 10 4,464 May-28-2019, 12:01 PM
Last Post: perfringo
  Split List and Sublist from Pyodbc parthi1705 1 2,232 May-05-2019, 10:44 AM
Last Post: Larz60+
  merging sublist into single list in python abhishek8singhai 8 9,468 Mar-22-2019, 11:46 PM
Last Post: micseydel
  Can I use a sublist as an argument in python? MartinBerlin 3 3,982 Aug-25-2018, 10:46 AM
Last Post: buran

Forum Jump:

User Panel Messages

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