2

I would like to state that I am new to programming and to Python. I did try doing research before posting this question but my lack of knowledge of even basics did not help yielding any results, thus requiring me to ask here.

I have two strings which are like:

 str_a = "100,101,105,304"
 str_b = "400,500,101"

I need to combine these strings into one so I use:

  str_c = str_a + "," + str_b

And my issue starts here. In this new string, where there are elements (numbers) separated by a comma, I want to have each item listed only once. The order of numbers do not matter but if it was ascending, it would be pretty amazing.

What can I do to combine these two, having each number listed once, if possible ordered small to large?

Since these are strings, I am not even sure if I can iterate through?

Your help will be greatly appreciated, thanks in advance.

Phil
  • 13,875
  • 21
  • 81
  • 126
  • You can iterate through strings in Python, i.e. `for i in string:`. Just a side note. – squiguy Oct 04 '12 at 22:55
  • What do you want at the end? A string? A list of the unique numbers that you can iterate through? Something else? – ernie Oct 04 '12 at 23:08

3 Answers3

5

Try this:

str_a = "100,101,105,304"
str_b = "400,500,101,2000"
l = str_a.split(',') + str_b.split(',')
print ','.join(sorted(set(l), key=int))

The output is:

100,101,105,304,400,500,2000

Thanks Oren for the comment! I've added key=int as an extra argument to sorted to compare the elements of the list as integers instead of strings. This argument allows one to specify a function of one argument that will be called on each element of the list to extract a comparison key. In our case, we use int to convert each element to an integer.

Anton Beloglazov
  • 4,939
  • 1
  • 21
  • 9
  • 2
    Notice that the sorting compares the parts as strings (i.e 'a' < 'b' and '1111' < '2'), without evaluating the numbers. In the case of only 3-digits numbers it works fine, but a more generic solution would be: `print ','.join(sorted(set(l), key = int))` – Oren Oct 04 '12 at 23:02
  • 2
    For a bit more detail, the general approach here is that each string is converted a list, splitting on the comma. The two are then combined. `set()` removes the duplicate elements, `sorted()` sorts, and the `join()` combines it all back into a comma separated string – ernie Oct 04 '12 at 23:11
  • 1
    Thank you Anton for this great simple and elegant solution. And thank you Oren and Ernie for your kind explanations. Really appreciate it ernie for taking your time. Thanks a lot. And Oren, I will check that key thing, it seems very interesting. – Phil Oct 04 '12 at 23:20
1

You want to split the strings up using the split method:

str_a_vals = str_a.split(",")
str_b_vals = str_b.split(",")

Then do:

allVals = str_a_vals
allVals += str_b_vals

str_c = ",".join(set(allVals))
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • why `.extend` ?? wouldn't it be better to just append the list/set with `str_a_vals + str_b_vals`? – alvas Oct 04 '12 at 23:14
  • Wasn't familiar with that syntax. – CrazyCasta Oct 04 '12 at 23:17
  • Thank you crazycasta for your answer and taking your time. Appreciate it! – Phil Oct 04 '12 at 23:24
  • @CrazyCasta The `+` operator works the same way for appends in python. But somehow i think it's personal preference to choose `extend` over `append` or vice versa. http://stackoverflow.com/questions/252703/python-append-vs-extend – alvas Oct 06 '12 at 00:36
  • @2er0 There is a big difference between `extend` and `append`. The `+` operator does a local extend (`extend` works on the list and doesn't return a value, `+` creates a new object and returns it). The difference between `extend` and `append` is that `extend` adds the elements of the list to the list whereas `append` adds the list itself to the list (ends up w/ [1,2,[3,4]]). – CrazyCasta Oct 06 '12 at 00:46
0

If you want to do stuff like sorting and selecting numbers, storing the numbers in strings is probably not the right approach. Try using lists, like this:

list_a = [100,101,105,304]
list_b = [400,500,101]

Getting the unique elements of the combined lists would be as easy as

unique = set(list_a + list_b)

and getting them in ascending order is

ascending = sorted(list_a + list_b)
Junuxx
  • 14,011
  • 5
  • 41
  • 71