-1

UPDATE: Okay, I did a complete overhaul of this question. Same question, but now I implemented my own code.

To start this off, all a, b, c, d and e are columns within a data frame that I will be doing a permutation of only 3 combinations at a time like the following:

data = list(iter.permutations([a, b, c, d, e], 3)

Then, I do the following to use a specific row within my data frame to find a single point on my graph:

specData = list(iter.permutations(spec[a.name], spec[b.name], spec[c.name], 
                                  spec[d.name], spec[e.name], 3)

Then, I will loop to display every possible graph due to my permutation. All of this works in my code and displays them correctly. I left out the non-needed part which is creating the format of the graph since none of that actually matters in this question:

for i in range(len(data)):

    plt.scatter(data[i][0] - data[i][1], data[i][0] - data[I][2], edgecolors=‘b’)

    plt.scatter(specData[i][0] - specData[i][1], specData[i][0] - specData[i][2], 
                edgecolors=‘r’)

On my previous graphs using data frames, I was able to display the names of the labels by creating them like this and graphing them one-by-one:

    plt.xlabel(a.name + ‘-‘ + b.name)
    plt.ylabel(a.name + ‘-‘ + c.name)

The output from the labels above would look like this on the graph's x and y labels:

a-b
a-c

Now, I'm not sure how to display names with a list like I do with a data frame, especially when its going to be "random" each time I loop a new tuple within a list, so I can't just hard-code it to be a, b, c, d, or e since I have no idea. I was assuming to use a dictionary, but I’m not sure how I would go about it.

researchnewbie
  • 100
  • 1
  • 10
  • 1
    Is each permutation always a, b or c? Just different ordering each time? – dijksterhuis Oct 17 '19 at 05:47
  • @dijksterhuis There are 5, so lets say a, b, c, d, and e. Edit: Okay, sorry if that last wasn't clear. There are 5 sets: a, b, c, d, and e and 3 combinations of each of them. – researchnewbie Oct 17 '19 at 05:50
  • You question is to broad. First you ask how to print and after editing you ask about a plot. Make two separate questions. – Mykola Zotko Oct 17 '19 at 06:05
  • @MykolaZotko Its just the print. The plot was an example of how I would use this. Plotting the name is the same as printing the name for my code. My apologies for the confusion. – researchnewbie Oct 17 '19 at 06:07

4 Answers4

2

You can zip a named_list with your data and assign each element of data a name.

Code:

from string import ascii_lowercase


def do():
    data = [[(0, 1, 2), (3, 4, 5), (6, 7, 8)],
            [(0, 1, 2), (6, 7, 8), (3, 4, 5)],
            [(3, 4, 5), (6, 7, 8), (0, 1, 2)]]

    featured_list = list(map(lambda x: list(zip(ascii_lowercase, x)), data))

    for items in featured_list:
        for item in items:
            print(f"{item[0]}: {item[1]}")


if __name__ == '__main__':
    do()

Output:

a: (0, 1, 2)
b: (3, 4, 5)
c: (6, 7, 8)
a: (0, 1, 2)
b: (6, 7, 8)
c: (3, 4, 5)
a: (3, 4, 5)
b: (6, 7, 8)
c: (0, 1, 2)
Phoenix
  • 3,996
  • 4
  • 29
  • 40
0

Try this:

from string import ascii_lowercase
alphabet=0
for i in data:
   for j in i:   
       print(ascii_lowercase[alphabet] + " : " + str(j))
       alphabet += 1

I have some more questions though like what if your list's length exceeds alphabet size? Also, this answer is valid if you just want to print and not store.

Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73
Pratik
  • 1,351
  • 1
  • 20
  • 37
  • 2
    `data[i]` is not a valid element. In this case in the first loop `i` would be `[(0, 1, 2), (3, 4, 5), (6, 7, 8)]` and `j` would try to take the value of `data[[(0, 1, 2), (3, 4, 5), (6, 7, 8)]]`. I think you wanted to say `for j in i:` which would be `(0,1,2)` – Kostas Charitidis Oct 17 '19 at 05:57
0

Not sure if that's what you are looking for but you can match each tuple as a key and add values as you like. i.e.:

data = [[(0, 1, 2), (3, 4, 5), (6, 7, 8)], 
        [(0, 1, 2), (6, 7, 8), (3, 4, 5)], 
        [(3, 4, 5), (6, 7, 8), (0, 1, 2)]]

dict1 = {(0, 1, 2): 'a',  (3, 4, 5): 'b',  (6, 7, 8): 'c'}

for row in data:
    print(', '.join(['%s:%s' % (dict1[elem], elem) for elem in row if elem in dict1]))
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • I do want it somewhat like this, however, I think having two for-loops isn't the best idea. Can I just create a dictionary alone without have a list to it? – researchnewbie Oct 17 '19 at 05:59
0

You can use the function chain.from_iterable() to iterate over each element in each sublist:

from itertools import chain
from string import ascii_lowercase

c = chain.from_iterable(data)

for i, j in zip(ascii_lowercase, c):
    print(i, j, sep = ' : ')

If you want to build a dictionary you can use the functions dict() and zip():

c = chain.from_iterable(data)
dct = dict(zip(ascii_lowercase, c))
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73