有人可以告诉我如何循环浏览这些地方,而不是通过继续附加到字符串的字符串将其列出来。
world = ["Japan", "China", "Seattle", "Brazil"]
print "This is my story of the following places: "
for place in world:
print place
编辑:我在打印命令的末尾添加了一个逗号,但由于它是一个单独的列表,我希望能够添加一个分隔符。 最终结果我希望它显示如下:
This is my store of the following places. Japan, China, Seattle, Brazil.
最终修改:使用以下代码,它会删除一行,我正在尝试找出如何删除其他行,以便它继续在同一行。
world = ["Japan", "China", "Seattle", "Brazil"]
print "This is my story of the following places: ",
for i in range(0,len(world)):
if i==(len(world)-1):
print world[i] + '.'
else:
print world[i] +',',
print ". Which has all
这是我访问过以下地方的故事。他们是中国,日本,西雅图和巴西
。其中包含所有伟大的图标
答案 0 :(得分:6)
使用join()
方法获取要与给定字符串连接的事物列表
print "This is my store of the following places. " + ", ".join(world)
答案 1 :(得分:1)
您必须在print()
命令后添加逗号以防止新行:
回复您的评论:
world = ["Japan", "China", "Seattle", "Brazil"]
print "This is my story of the following places: ",
for i in range(0,len(world)):
if i==(len(world)-1):
print world[i] + '.', #<-- Add another comma
else:
print world[i] +',',
输出:
This is my story of the following places: Japan, China, Seattle, Brazil.
我制作了if语句来添加逗号&#39;和一个期间&#39;使输出看起来像一个列表。这适用于任何尺寸列表。
但是,请查看join()
方法(由@ILostMySpoon建议)。效率更高。
答案 2 :(得分:0)
这是我修改过的python程序,因此您可以为从0到“ m”的整个集合计算乘法逆:
#This code was edited by David Pinos from cybersecurity Centennial College. Canada
# This code is contributed by Nikita tiwari and it was taked from https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/#:~:text=To%20find%20multiplicative%20inverse%20of,value%20of%20gcd%20as%201.&text=We%20can%20remove%20the%20second,0%20for%20an%20integer%20y.
# Iterative Python 3 program to find
# modular inverse using extended
# Euclid algorithm
# Returns modulo inverse of a with
# respect to m using extended Euclid
# Algorithm Assumption: a and m are
# coprimes, i.e., gcd(a, m) = 1
def modInverse(a, m) :
m0 = m
y = 0
x = 1
if (m == 1) :
return 0
while (a > 1) :
# q is quotient
if (m==0):
return "there is no inverse for this number"
else:
q = a // m
t = m
# m is remainder now, process
# same as Euclid's algo
m = a % m
a = t
t = y
# Update x and y
y = x - q * y
x = t
# Make x positive
if (x < 0) :
x = x + m0
return x
# Driver program to test above function
m = 26 #your SET
i=0
while (i<m):
print("Modular multiplicative inverse of ", i, " in ", m, " is: ", modInverse(i, m))
i=i+1