Ask Python about contextually changing values!

Asked 2 years ago, Updated 2 years ago, 16 views

The explanation may be confusing because of the complicated content, but I would appreciate your understanding

The 'value that changes according to the situation' that I want to ask is

For example,

x y z and

Suppose you have x'y'z' and you have six possible cases (3!)

When x = x'y=y'z=z', the value is 1

When x=x'y=z'z=y', the value is 2

When x=y'y=x'z=z', the value is 3

When x=y'y=z'z=x', the value is 4

When x=z'y=x'z=y', the value is 5

When x=z'y=y'z=x', the value is 6

Is there a way to set a value that changes according to the situation in this way?

If the number of cases is small, you can write it in an if statement, but

Even if there are more than 6 variables, there are 720 cases, so I can't write an if sentence one by one.

I'd like to know the solution to the price that comes from each situation changing.

Currently, the data given is

It's in the form of a table

That is,

                                    x          y        z

                          x         10       20        30

                          y         40       50        60

                          z         70      80         90   

It's organized like this.

In this situation, whenever xy changes places, the value can be changed to a coordinate form.

I wonder if there is a way.

When (ex, (x, y, z) changes to (y, x, z), the value is

                                    y        x        z

                          y        50        40        60

                          x         20       10        30

                          z         80       70        90 

It'll be created like this, right? Can I organize these values into a list?

I'm sorry to ask you such a complicated and difficult question

python

2022-09-21 19:25

1 Answers

I don't know if you'll see it because it's a late reply I am writing an answer because I am using Pandas well these days.

As Young Hoon Jung replied, it is really easy to use Pandas for the above questions.

import pandas as pd

raw_data = {'x': [10, 40, 70],
            'y': [20, 50, 80],
            'z': [30, 60, 90]}
cur_xy = ['x', 'y', 'z']
df = pd.DataFrame(raw_data, index=cur_xy, columns=cur_xy)
print('Given Materials\n', df)

new_xy = ['y', 'x', 'z']
df = df.reindex(new_xy, columns=new_xy)
print('\nChanged Materials\n', df)

It's easy, right?

** The hash code executor does not recognize the pandas, so it cannot be executed by the code executor. It works when I set it to Python 3 --; I'll upload the result of the replay.

Given data
     x   y   z
x  10  20  30
y  40  50  60
z  70  80  90

Changed data
     y   x   z
y  50  40  60
x  20  10  30
z  80  70  90


2022-09-21 19:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.