0

What I'm trying to do is to print the reverse number of the input. input = "1 3 4 2" output = "2 4 3 1"

I want to know why my code show this error:

if __name__ == "__main__":
    n = 4
    arr = map(int, "1 3 4 2".split())
    ar = list(arr)
    ar.reverse()
    string = ' '
    string.join(ar)
    print(string)

Error: string.join(ar) TypeError: sequence item 0: expected str instance, int found

and if i change the map argument from int to str, it shows no result.

4
  • Remove the map(int part and it should work. Commented Nov 22, 2019 at 5:08
  • Use a list comprehension instead of doing list(map()), eh. It also looks like you’re not saving the result of your join anywhere. Commented Nov 22, 2019 at 5:11
  • You are trying to join int with string, better to remove int Commented Nov 22, 2019 at 5:11
  • arr = map(int, "1 3 4 2".split()) the "1 3 4 2" should be the input with that format. I simplified the code and tried to understand python. that's why i need the map() and turn it into a list. Commented Nov 22, 2019 at 6:34

7 Answers 7

2

You should use much simpler approach:

>>> ''.join(reversed('1 2 3 4'))
'4 3 2 1'
Sign up to request clarification or add additional context in comments.

Comments

1
n = 4
arr = map(int, "1 3 4 2".split())
ar = list(arr)
b=ar.reverse()
string =' '
string=string.join(str(ar))
print(string)

There are some mistakes in your code when you use map with int as a parameter the string is splitted to integers and the join function expects strings

One more thing the string.join function does not change the value of the string it just computes the value. If you want your strings value to be changed you should assign explicitly as in the code I have provided

Comments

1

You clearly need to join list of numbers. So unlike what others suggest, map(int, list) is correct.

sorting of list of strings that have number is not the same as sorting of list of strings.

That is, [121,17,11] will be [11,17,121] when you sort. But ["121","17","11"] will be ["11",121","17"]

>>> n = 4
>>> arr = map(int, "1 3 4 2".split())
>>> ar = list(arr)
>>> ar.reverse()
>>> string = ' '
>>> string.join(ar)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> ar
[2, 4, 3, 1]
>>> map(str,ar)
['2', '4', '3', '1']

So you need to convert the list of int back to list of string and then join

>>> string.join(map(str,ar))
'2 4 3 1'
>>> print(string)

2 Comments

just to clarify.. so we can't join list of int to string?
you can extend a list of int and strings. but when you want to join them as a single string, you need to convert all ints to strings and then join.
0

You are trying to join an int object, for your piece of code to work

you will have to replace your 3 line.

arr = map(str, "1 3 4 2".split())

Comments

0

Use the below method to join the elements:

string.join(str(x) for x in ar)

Comments

0

Looks like you solved the initial error of int vs str, nice work. Now, string.join will return a new string, not modify the existing one in place.

So

n = 4 
arr = map(str, "1 3 4 2".split()) 
ar = list(arr) 
ar.reverse() 
string = ' ' 
new_string = string.join(ar) 
print(new_string)

returns

2 4 3 1

There are some cleaner ways to do what this is doing. For example

reversed_str = "".join(reversed("1 3 4 2"))
print(reversed_str)

2 Comments

why do you need map(str for something that's already a string?
@lenik you don't. I was just using the provided code to explain why the solution didn't work as expected. I edited the answer to add a cleaner way of doing it. Looks like you did too
0

in list, you can get a reversed list using list(reversed(array)). and to convert list to string, you can do this: some_str.join(str(x) for x in arr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.