英文:
How can I access the value of an iterator?
问题
我一直在尝试将一个字符串分成不同的部分,以便进行比较。我想根据在计算机开头设置的初始列表中的row中的元素的存在来执行此操作。以下是代码,无论我做什么,似乎全局值"Cabin"都没有定义,我无法访问它的值,这对我来说是必要的,以便我可以在最后一行进行分割。
for row in SH_vars_dict:
if any(Cabin in row for Cabin in Cabins):
split_SH = row.split(Cabin+"_")
SH_vars_dict_short.append(split_SH[1])
else:
SH_vars_dict_short.append(row)
split_SH = row.split("_")
ED_SH_thr.append(split_SH[0])
有没有办法访问该迭代器的值?提前感谢!
我基于一个特定值执行了拆分,但后来我意识到代码中出现了同一数组的多个值。每当我尝试声明这个值时,程序会报告关于全局名称'Cabin'未定义的前述错误。
英文:
I have been trying to split a string into different pieces in order to perform comparisons. I want to do so based on the existence of an element in row which is located in an initial list set at the beginning of the computer. The code is the following one, no matter what I do, it appears that the global value "Cabin" is not defined and I cannot access its value, which is the necessary one for me to make a partition on the last line
for row in SH_vars_dict:
if any(Cabin in row for Cabin in Cabins):
split_SH = row.split(Cabin+"_")
SH_vars_dict_short.append(split_SH[1])
else:
SH_vars_dict_short.append(row)
split_SH = row.split("_")
ED_SH_thr.append(split_SH[0])
Is there any way to access the value of that iterator? Thanks in advance!
I performed the splits based on one particular value but then I realized that more than one value for the same array was arising on the code. Any time I try to declare this value the program reports the aforementioned error regarding the non-definition of the global name 'Cabin'
答案1
得分: 2
Cabin
不是全局变量;它是用作 any
参数的迭代表达式范围内的局部变量。
您可以使用赋值表达式来捕获使 Cabin in row
为真的 Cabin
的(第一个)值。
for row in SH_vars_dict:
if any((x := Cabin) in row for Cabin in Cabins):
split_SH = row.split(x+"_")
SH_vars_dict_short.append(split_SH[1])
else:
SH_vars_dict_short.append(row)
split_SH = row.split("_")
ED_SH_thr.append(split_SH[0])
有关有关 x
的作用域是如何确定的更多详细信息,请参阅PEP 572,“目标的作用域”。
英文:
Cabin
is not global; it's a local variable in the scope of the iterator expression used as the argument for any
.
You can use an assignment expression to capture the (first) value of Cabin
for which Cabin in row
is true.
for row in SH_vars_dict:
if any((x := Cabin) in row for Cabin in Cabins):
split_SH = row.split(x+"_")
SH_vars_dict_short.append(split_SH[1])
else:
SH_vars_dict_short.append(row)
split_SH = row.split("_")
ED_SH_thr.append(split_SH[0])
See PEP 572, "Scope of the target" for more details on how the scope of x
is determined.
答案2
得分: 0
你可以只使用常规的无聊for
循环:
for row in SH_vars_dict:
for Cabin in Cabins:
if Cabin in row:
...
英文:
You could just use regular boring for
loops:
for row in SH_vars_dict:
for Cabin in Cabins:
if Cabin in row:
...
答案3
得分: -2
在Python中,您可以使用next()函数访问迭代器的值。next()函数允许您从迭代器中检索下一个元素。
以下是如何访问迭代器值的示例:
numbers = [1, 2, 3, 4, 5]
iterator = iter(numbers)
# 访问第一个值
first_value = next(iterator)
print(first_value) # 输出:1
# 访问第二个值
second_value = next(iterator)
print(second_value) # 输出:2
# 使用循环访问剩余的值
for value in iterator:
print(value) # 输出:3, 4, 5
英文:
In Python, you can access the value of an iterator using the next() function. The next() function allows you to retrieve the next element from an iterator.
Here is an example to how to access the value of an iterator:
numbers = [1, 2, 3, 4, 5]
iterator = iter(numbers)
# Accessing the first value
first_value = next(iterator)
print(first_value) # Output: 1
# Accessing the second value
second_value = next(iterator)
print(second_value) # Output: 2
# Accessing the remaining values using a loop
for value in iterator:
print(value) # Output: 3, 4, 5
答案4
得分: -3
你遇到的错误是由于代码中变量的作用域限制引起的。在Python中,在循环内定义的变量仅在该循环内部可访问,无法在循环外部访问。在你的情况下,变量Cabin在循环内部定义,因此无法在split_SH行之外访问。
要解决此问题,你可以在循环之前将Cabin的值分配给一个单独的变量,以便在循环内部访问它。以下是应该能够工作的代码的更新版本:
Cabin_value = None # 在循环之前初始化一个单独的变量来保存Cabin的值
for row in SH_vars_dict:
if any(Cabin in row for Cabin in Cabins):
Cabin_value = Cabin # 将当前的Cabin值分配给单独的变量
split_SH = row.split(Cabin_value + "")
SH_vars_dict_short.append(split_SH[1])
else:
SH_vars_dict_short.append(row)
split_SH = row.split("")
ED_SH_thr.append(split_SH[0])
通过在循环之前将Cabin的值分配给Cabin_value,你可以在循环内部使用Cabin_value执行所需的字符串分割操作。
英文:
The error you're encountering is due to the scoping of variables in your code. In Python, variables defined within a loop are local to that loop and not accessible outside of it. In your case, the variable Cabin is defined within the loop, so it's not accessible in the split_SH line.
To resolve this issue, you can assign the value of Cabin to a separate variable before the loop, so that it can be accessed inside the loop. Here's an updated version of your code that should work:
Cabin_value = None # Initialize a separate variable to hold the value of Cabin
for row in SH_vars_dict:
if any(Cabin in row for Cabin in Cabins):
Cabin_value = Cabin # Assign the current value of Cabin to the separate variable
split_SH = row.split(Cabin_value+"")
SH_vars_dict_short.append(split_SH[1])
else:
SH_vars_dict_short.append(row)
split_SH = row.split("")
ED_SH_thr.append(split_SH[0])
By assigning the value of Cabin to Cabin_value before the loop, you can then use Cabin_value within the loop to perform the desired string split operation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论