0

I have a list of columns fields: ['AGE', 'NAME', 'COUNTRY']

There is also list of lists:

lines = [['A', 'B', 'C'],
         ['A', 'B', 'C', 'D']]

I need to assign to each column a field value. It must be prompt:

Line 0:
A -> choose one value from list [AGE, NAME, COUNTRY] 
B -> choose one value from list [AGE, NAME, COUNTRY] 
C -> choose one value from list [AGE, NAME, COUNTRY] 

Line 1:
A -> choose one value from list [AGE, NAME, COUNTRY] 
B -> choose one value from list [AGE, NAME, COUNTRY] 
C -> choose one value from list [AGE, NAME, COUNTRY] 

How to do that using Python?

I tried to iterate list of lines first:

res = []
for line in lines:
  for cols in line:
    fieldname = input('Choose field name') # here I need to provide value from `fields`
    res[line] = cols

As result I need:

lines = [["AGE", "NAME", "COUNTRY"],
         ["COUNTRY", "AGE", "NAME", "NONE"]]
Mandalina
  • 87
  • 5
  • 1
    You forgot to post your attempt to solve this problem. – Scott Hunter May 25 '22 at 12:18
  • Yes, I will do, – Mandalina May 25 '22 at 12:18
  • 1
    I'm not sure I understand what is your question. Asking for the user input is done with the `input` function. What do you intend to do with that input? What is your expected output? Also please post a clear [mre]. `A`, `B` and `C` are not defined here... – Tomerikoo May 25 '22 at 12:22
  • I just need to create a new lis that represents fields name for each column in lines list: – Mandalina May 25 '22 at 12:30
  • If your final output is a list of lists, why do you set `res` as a dict? You need to set `res` as an empty list (`res = []`). Then for each `line` create a new empty list for that line (`new_line = []`). Then for each col add the value (`new_line.append(fieldname)`). Finally add the new row to the list after the columns loop (`res.append(new_line)`) – Tomerikoo May 25 '22 at 12:32
  • Yes, you are right, but I need to provide a choose value by user from existing fields – Mandalina May 25 '22 at 12:52

1 Answers1

0

It seems to me that you are on the right track with the loops, so first you go through each of the lines, and then go through each of the columns asking the user to input a specific value for it:

fields = ["AGE", "NAME", "COUNTRY", "NONE"]
lines = [['A', 'B', 'C'],
         ['A', 'B', 'C', 'D']]

res = []
for i, line in enumerate(lines):
    print(f"Line {i}:")
    vals = []
    for col in line:
        field_name = input(f"{col} -> choose one value from list {fields}: ")
        vals.append(field_name)
    print()
    res.append(vals)
print(res)

Output:

Line 0:
A -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: AGE
B -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: NAME
C -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: COUNTRY

Line 1:
A -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: COUNTRY
B -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: AGE
C -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: NAME
D -> choose one value from list ['AGE', 'NAME', 'COUNTRY', 'NONE']: NONE

[['AGE', 'NAME', 'COUNTRY'], ['COUNTRY', 'AGE', 'NAME', 'NONE']]
Chris
  • 1,206
  • 2
  • 15
  • 35
  • Thank you a lot, is it possible to select value like menu item from keyboard? – Mandalina May 25 '22 at 13:36
  • Hmm, perhaps this question can help you with that: https://stackoverflow.com/questions/56723852/console-select-menu-in-python? – Chris May 25 '22 at 13:44