Home > OS >  create a list by concatenating each individual string input with digits ranging from 1 to n. For out
create a list by concatenating each individual string input with digits ranging from 1 to n. For out

Time:08-16

Example:

Input: i j k

Enter a value for n = 4

Output: I1 I2 I3 I4 J1 J2 J3 J4 K1 K2 K3 K4

I can't seem to separate the word and the number. I am getting

I 1 I 2 I 3 I 4 J 1 J 2 J 3 J 4 K 1 K 2 K 3 K 4 

instead of

I1 I2 I3 I4 J1 J2 J3 J4 K1 K2 K3 K4 
my_list = input("Input: ")
up_list = my_list.upper()
var =''
new_list2 = ""
lists = up_list.split()
n = int(input("Enter a value for n: "))
for x in lists:
    for y in range(1, n 1):
        w = str(y)
        new_list = ('{}{}'.format(x, y))
        new_list2  = new_list
for i in new_list2:
    var  = i
    var  = " "
print(var)

CodePudding user response:

Use list comprehension:

lst = input("Input: ").upper().split()
n = int(input("Enter a value for n: "))
pairs = ' '.join(f'{s}{i}' for s in lst for i in range(1, n 1))
print(pairs)
# I1 I2 I3 I4 J1 J2 J3 J4 K1 K2 K3 K4

CodePudding user response:

In your second for loop:

for i in new_list2:
var  = i
var  = " "

you could do something like this so every other has the space instead:

for i in range(len(new_list2)):
var  = new_list2[i]
    if i % 2 == 0:
       var  = " "

CodePudding user response:

If I start from your code, the problem is due only to the two lines with new_list2. It should be a list (not a string), and values should be appended (not added). Here is the new code, very close to yours. However, you should really take care about the correspondence between the type and the name and each variable.

my_list = input("Input: ")
up_list = my_list.upper()
var =''
new_list2 = [] # instead of new_list2 = ""
lists = up_list.split()

n = int(input("Enter a value for n: "))

for x in lists:
    for y in range(1, n 1):
        w = str(y)
        new_list = ('{}{}'.format(x, y))
        new_list2.append(new_list) # instead of new_list2  = new_list
for i in new_list2:
    print(i)
    var  = i
    var  = " "
print(var) # I1 I2 I3 I4 J1 J2 J3 J4 K1 K2 K3 K4 

Other answers will give you more pyhtonic and optimized code.

CodePudding user response:

my_list and up_list and new_list2 are strings, not lists. In the interest of clarity, you should give your variables names that describe the purpose of the variable.

Also, you create each element of your output in new_list, then append it to the string new_list2, then iterate over each character of this string and append it to the string var with spaces between them. This makes no sense at all. Instead, you can simply create a list that you fill up in your nested loop, and then print that list separated by spaces using str.join, or by unpacking the list into the print function and specifying the separator.

Consider the following:

letters = input("Input: ").upper().split()
n = int(input("Enter a value for n: "))

result = []                 # Create an empty LIST
for letter in letters:
    for i in range(1, n 1):
        result.append(f"{letter}{i}")

print(" ".join(result))
# or
print(*result, sep=" ")

You can condense the nested loops into a list comprehension like so:

result = [f"{letter}{i}" for letter in letters for i in range(1, n 1)]

Or, if you only want to print this, you could just use the generator expression that drives the list comprehension as an argument to str.join:

print(" ".join(f"{letter}{i}" for letter in letters for i in range(1, n 1)))

CodePudding user response:

letters = input("Type letters:")
letters = letters.upper()
letter_list = letters.split(" ")

n = int(input("Enter a value for n: "))  # assuming you will only input an integer here

for letter in letter_list:
    for i in range(1, n 1):
        print(f"{letter}{i}", end=" ")

Alternatively, if you are trying to save the string for whatever reason, this will work too:

letters = input("Type letters:")
letters = letters.upper()
letter_list = letters.split(" ")

n = int(input("Enter a value for n: "))

my_str = ""

for letter in letter_list:
    for i in range(1, n 1):
        my_str  = f"{letter}{i} "
print(my_str)
  • Related