0

Some context: I have a dataset from 14 samples that each are comprised of two duplicate parts. Each duplicate part of each sample has sub-samples, and each of these have a location in which can be described by an x and y position where n x=10 and n y=10 (100 positions, per 2 duplicates, per 14 samples = 2800 Unique IDs).

So, each sub-sample has a unique ID that is comprised of sample'+'part'+'locationx'+'locationy. Note this is not a product (I think!?) but a joined string comprised of each possible combination. Each unique ID has considerable amounts of numerical data attached to it. I wish to generate a list of the unique ID of each sub-sample, so that I can then use this to loop code efficiently. The unique IDs would be tedious to write out, for instance as a nested list.

I have read many posts about how to add a string to each element in a list. This can be achieved by a List Comprehension. That would speed things up. But, what about if you want to add each string in a list to each string in another list? That would speed things up mightily!

This sounds a little like building a multidimensional array, which can be achieved using lists, but not if those lists are strings (it seems to me!), only integers. I hope I'm wrong and someone can tell me so.

A downsized description of the concept using just two lists:

    list1 = ['foo', 'fob', 'faz']
    list2 = ['bar', 'bat', 'bump']

magic

    list3 = ['foobar', 'fobbar', 'fazbar', 'foobat', 'fobbat', 'fazbat', 'foobump', 'fobbump', 'fazbump']
matt
  • 1
  • 1
  • 1
    Briefly: join each result of the Cartesian product. – TigerhawkT3 Mar 22 '21 at 02:18
  • @Ann I think ``'sample'+'part'+'locationx'+'locationy'`` was supposed to be `sample+part+locationx+locationy` – wjandrea Mar 22 '21 at 02:19
  • @wjandrea Oh, you're right, sorry :) – Red Mar 22 '21 at 02:21
  • Thanks so much, but yes I do want to join the results @TigerhawkT3, so I've modified the question. Pointing to [this question](https://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists) really helped me. But, I'm after a list of all possible joined combinations. Do I put a join function somewhere? – matt Mar 22 '21 at 10:50
  • You can concatenate strings with the `+` operator, or concatenate an iterable containing strings together with `str.join`. – TigerhawkT3 Mar 22 '21 at 10:54
  • TigerhawkT3 this is excellent, many thanks. I'll go and try this now. – matt Mar 22 '21 at 10:56
  • TigerhawkT3 it works with an append command, supplemented by a strip UniqueIDs = [] >>> for i in UniqueID_tuples: ... UniqueIDs.append(''.join(i).strip()) no doubt this is not news to you, but wanted to say thanks again. – matt Mar 22 '21 at 11:35

0 Answers0