简体   繁体   English

如何在同一行打印列表值?

[英]How to print list value in same line?

How Can i print only value of list in same line ?如何在同一行中仅打印列表的值? My Code is:我的代码是:

t = int(input())
for case_no in range(1, t+1):
    n = int(input())
    li = list()
    for i in range(1, n+1):
        if n % i == 0:
            li.append(i)
    print("Case {}: {}".format(case_no, li, sep=' ', end=''))

My Input Output Sample Result:我的输入输出示例结果:

2
6
Case 1: [1, 2, 3, 6]
23
Case 2: [1, 23]

My Expected Output:我的预期输出:

2
6
Case 1: 1 2 3 6
23
Case 2: 1 23

Try join() to convert your list to string -尝试join()将您的列表转换为字符串 -

" ".join(map(str, li))

Here your list is a list of integers and join() joins every string element of an iterable separated by some string (in this case " ").这里您的列表是一个整数列表, join()连接由某个字符串(在本例中为“”)分隔的可迭代对象的每个字符串元素。 So, you need to convert each integer to a string first.因此,您需要先将每个整数转换为字符串。 map() can do that. map()可以做到这一点。 map() takes a function and apply it on each element of an iterable which is passes as the second arguement (here list li ). map()接受一个函数并将其应用于作为第二个参数传递的可迭代对象的每个元素(此处为列表li )。 So, map(str, li) will create an iterable where each element is a string representation of every element of list li因此, map(str, li)将创建一个可迭代对象,其中每个元素都是列表li中每个元素的字符串表示

So, your line would be-所以,你的台词是——

print("Case {}: {}".format(case_no, " ".join(map(str, li)), sep=' ', end=''))

You can use generator expression for that too -您也可以使用生成器表达式 -

print("Case {}: {}".format(case_no, " ".join(str(i) for i in li), sep=' ', end=''))

In this case you are using a generator expression (think of it like list comprehension if you don't know).在这种情况下,您使用的是生成器表达式(如果您不知道,可以将其视为列表理解)。 The generator expresssion iterates over every element of li and returns a stringified (don't know if that's the word) version of that element.生成器表达式迭代li每个元素并返回该元素的字符串化(不知道是不是这个词)版本。

Also, if you just want to print the list and not maintain it for other purposes then you can just simplify it a bit -另外,如果您只想打印列表而不是出于其他目的维护它,那么您可以稍微简化一下 -

t = int(input())
for case_no in range(1, t+1):
    n = int(input())
    print("Case {}: {}".format(case_no, " ".join(str(i) for i in range(1, n+1) if not n % i), sep=' ', end=''))

You can use print to do it:您可以使用打印来做到这一点:

data = [[1,2,3,4], [2,4,6]]

for idx,d in enumerate(data,1):
    print( f"Case {idx}:", *d)  # "splat" the list 

prints:印刷:

Case 1: 1 2 3 4
Case 2: 2 4 6

Using the splat ( * ) (or unpacking argument lists ) operator to provide all elements of your list as parameters to print:使用 splat ( * )(或解包参数列表)运算符将列表中的所有元素提供为要打印的参数:

print( *[1,2,3,4]) == print( 1, 2, 3, 4)

Either you make a string out of the list, either you need another loop to parse the list.要么从列表中创建一个字符串,要么需要另一个循环来解析列表。 There might be other solutions as well.可能还有其他解决方案。

You can use a comma after the print statement to avoid adding new line.您可以在打印语句后使用逗号以避免添加新行。

Making a string :制作字符串:

  print("Case {}: {}".format(case_no, "".join(str(li).split(',')).strip('[]'), sep=' ', end=''))

With loop:带循环:

print("Case {}: ".format(case_no)),
for i in li:
    print(i),
print

您可以将列表中的元素作为字符串li.append(str(i))然后加入" ".join(li)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM