我想创建一个函数,在数组中的每个字符串前面添加一个特定的单词.最后我想改变数组.我有这个代码:
def make_great(magicians):
"""Change magicians"""
for magician in magicians:
magician = "the Great" + magician
magicians = ["hudini", "angel", "teller", "anderson", "copperfield"]
make_great(magicians)
print(magicians)
Run Code Online (Sandbox Code Playgroud)
此代码不会更改数组.如何使我的功能工作?
我知道你不应该在迭代列表时添加/删除项目.但是,如果我不更改列表长度,我可以修改列表中的项目吗?
class Car(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return type(self).__name__ + "_" + self.name
my_cars = [Car("Ferrari"), Car("Mercedes"), Car("BMW")]
print(my_cars) # [Car_Ferrari, Car_Mercedes, Car_BMW]
for car in my_cars:
car.name = "Moskvich"
print(my_cars) # [Car_Moskvich, Car_Moskvich, Car_Moskvich]
Run Code Online (Sandbox Code Playgroud)
或者我应该迭代列表索引而不是?像那样:
for car_id in range(len(my_cars)):
my_cars[car_id].name = "Moskvich"
Run Code Online (Sandbox Code Playgroud)
问题是:上述两种方式是允许的,还是只有第二种方式没有错误?
如果答案是肯定的,那么以下代码段是否有效?
lovely_numbers = [[41, 32, 17], [26, 55]]
for numbers_pair in lovely_numbers:
numbers_pair.pop()
print(lovely_numbers) # [[41, 32], [26]]
Run Code Online (Sandbox Code Playgroud)
UPD.我想看看python文档,它说"允许这些操作",而不是某人的假设.
Stack Overflow 上有很多关于这个一般主题的问答,但它们要么质量很差(通常是初学者的调试问题暗示的),要么以其他方式错过了目标(通常是不够通用)。至少有两种极其常见的方法会使幼稚的代码出错,初学者从关于循环的规范中获益更多,而不是从将问题作为拼写错误或关于打印所需内容的规范中获益。所以这是我尝试将所有相关信息放在同一个地方。
假设我有一些简单的代码,可以对一个值进行计算x并将其分配给y:
y = x + 1
# Or it could be in a function:
def calc_y(an_x):
return an_x + 1
Run Code Online (Sandbox Code Playgroud)
现在我想重复计算 的许多可能值x。我知道for如果我已经有要使用的值列表(或其他序列),我可以使用循环:
xs = [1, 3, 5]
for x in xs:
y = x + 1
Run Code Online (Sandbox Code Playgroud)
while或者,如果有其他逻辑来计算值序列,我可以使用循环x:
def next_collatz(value):
if value % 2 == 0:
return value // 2
else:
return 3 * value + 1
def collatz_from_19():
x = 19
while x != 1:
x …Run Code Online (Sandbox Code Playgroud) 这是一个有效的python行为吗?我认为最终结果应该是[0,0,0]并且id()函数应该在每次迭代时返回相同的值.如何使它成为pythonic,而不是使用枚举或范围(len(bar))?
bar = [1,2,3]
print bar
for foo in bar:
print id (foo)
foo=0
print id(foo)
print bar
Run Code Online (Sandbox Code Playgroud)
输出:
[1, 2, 3]
5169664
5169676
5169652
5169676
5169640
5169676
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud) 据我所知,在Python中,常规的c ++样式变量赋值被替换为对东西的引用即
a=[1,2,3]
b=a
a.append(4)
print(b) #gives [1,2,3,4]
print(a) #gives [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
但我仍然困惑为什么与基本类型的类似情况,例如.整数的工作方式不同?
a=1
b=a
a+=1
print(b) # gives 1
print(a) # gives 2
Run Code Online (Sandbox Code Playgroud)
但等等,当我们考虑循环时,它会变得更加混乱!
li=[1,2,3]
for x in li:
x+=1
print(li) #gives [1,2,3]
Run Code Online (Sandbox Code Playgroud)
这是我的预期,但如果我们这样做会发生什么:
a,b,c=1,2,3
li=[a,b,c]
for x in li:
x+=1
print(li) #gives [1,2,3]
Run Code Online (Sandbox Code Playgroud)
也许我的问题应该是如何遍历整数列表并在没有map()的情况下更改它们,因为我需要一个if语句.我唯一能做的就是没用
for x in range(len(li)):
Do stuff to li[x]
Run Code Online (Sandbox Code Playgroud)
将整数打包在一个元素列表中.但必须有更好的方法.
my_list = [1, 2]
def f():
print my_list
yield 11
print my_list
yield 22
print my_list
my_list[:] = f()
print "finally ",
print my_list
Run Code Online (Sandbox Code Playgroud)
输出:
[1, 2]
[1, 2]
[1, 2]
finally [11, 22]
Run Code Online (Sandbox Code Playgroud)
我的期望是:
[1, 2]
[11, 2]
[11, 22]
finally [11, 22]
Run Code Online (Sandbox Code Playgroud)
有人曾告诉我切片分配到位.显然不是.是否有一种优雅的方式来实现它?
我有以下代码片段:
d = [[0,558,437,142,45,290], [558,0,232,500,600,523], [437,232,0,408,492,572],
[142,500,408,0,180,197], [45,600,492,180,0,254], [290,523,572,197,254,0]]
for row in d:
for elem in row:
elem = elem * 2
print d[0][1]
print '\n'
Run Code Online (Sandbox Code Playgroud)
为什么它不打印新值,它仍然打印558