在python中,['a'] * 4给出['a', 'a', 'a', 'a']。为什么不[['a'], ['a'], ['a'], ['a']]呢?
我正在解决这个挑战,其中根据给定的数字,它将显示每个奇数的星号(*)增量。基本上结果应该是,如果我运行下面的函数并将 n_floor 设置为 5,它应该显示下面的结果。我的代码以某种方式迭代每个楼层数并增加每个奇数的 * 但结果失败,但这是因为这些引号之间的星号间距是错误的。有什么想法/提示如何解决这个问题吗?
A。正确结果
[' * ', ' *** ', ' ***** ', ' ******* ', '*********']
Run Code Online (Sandbox Code Playgroud)
b. 我的脚本的结果如下:
['*', '***', '*****', '*******', '*********']
Run Code Online (Sandbox Code Playgroud)
def tower_builder(n_floor):
a = '*'
b = 1
c= 0
result = []
num=range(1, n_floor+1)
# * to increment by odd number
for x in num:
c = a
result.append(c)
a += str('**')
return result
Run Code Online (Sandbox Code Playgroud) 我正在使用 Python 3.7 并且对 tuple() 感到困惑。有时它会重新排序数据,有时不会:
>>> a=tuple([2, 1, 3])
>>> print(a)
(2, 1, 3) <== the tuple from list is not re-ordered
>>> s={2, 1, 3}
>>> b=tuple(s)
>>> print(b)
(1, 2, 3) <== the tuple from set is re-ordered
>>> print(tuple({10, 5, 30}))
(10, 5, 30) <== the tuple from set is not re-ordered
>>> print(s)
{1, 2, 3} <== the set itself is re-ordered
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
tuple() 的预期行为是什么:
1.1 生成有序元组?
1.2 修改输入?
我在哪里可以找到最终文档?我检查了 Python 在线文档https://docs.python.org/3/library/stdtypes.html#tuple,它根本不提供此类信息。
谢谢。
正如标题所述,我想找出整数i是否可以被列表中的所有元素整除。
我有这个:
db = [3, 4, 5, 6, 7, 8]
i = 1
if all(i % d != 0 for d in db):
i += 1
Run Code Online (Sandbox Code Playgroud)
我尝试使用带有 的if循环all(),但即使满足条件,它也只是跳过了该部分(我认为)。它应该只是添加到整数,i直到它可以被 3、4、5、6、7 和 8 整除。
任何提示或我做错了什么?
谢谢,本
说我有一个清单
list = ['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
我想创建以下字符串:
foo_a = bar['a']
foo_b = bar['b']
foo_c = bar['c']
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
for i in list:
print("foo_{} = bar['{{}}']".format(i))
Run Code Online (Sandbox Code Playgroud)
但输出是
foo_a = bar['{}']
foo_b = bar['{}']
foo_c = bar['{}']
Run Code Online (Sandbox Code Playgroud)
我读过为什么 python F 字符串插值用引号引起来?但第二种方法似乎不再起作用了。
我想根据我拥有的三个列表创建一个字典:
Hier = ['A', 'C']
values = [10, 20]
All = ['A', 'B', 'C', 'D']
Run Code Online (Sandbox Code Playgroud)
结果应如下所示:
{"A", 10, "B": "", "C": 20, "D":""}
Run Code Online (Sandbox Code Playgroud) 多年来,我每天都使用 Pandas,并且经常(但不是那么频繁)使用 Numpy。大多数时候我会做类似的事情:
import pandas as pd
import numpy as np
Run Code Online (Sandbox Code Playgroud)
但是[编辑:在 pandas 2.0 之前] 还可以选择直接从 Pandas 使用 Numpy:
df['value'] = pd.np.where(df['date'] > '2020-01-01', 1, 0)
Run Code Online (Sandbox Code Playgroud)
有谁知道这些选项中的任何一个是否比另一个选项的性能明显更高?
在学习 udemy 课程后,我陷入了“'str object is not callable”错误。我基本上已经复制并粘贴了给我带来问题的代码片段,但它仍然产生相同的错误。还没有发现任何与我遇到的确切问题相关的内容,但如果这是一个简单得可笑的修复,我不会感到惊讶!
问题在于“ops_function”位抛出“str”对象不可调用错误。
如果您需要更多信息,请告诉我,感谢您的见解!
from art import logo
print(logo)
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
operations = {
"+": "add",
"-": "subtract",
"*": "multiply",
"/": "divide"
}
first = int(input("Enter a number: "))
second = int(input("Enter another number: "))
for op in operations:
print(op)
op_choice = input("Select an operator from the list above: …Run Code Online (Sandbox Code Playgroud) 我有一份物品清单。在 for 循环中,我检查该项目是否为 3。如果是 3,则应将 3 更改为 2。这是我想到的使用 for 循环的两种方法。但只有最后一个有效。
有没有办法让第一个 for 循环工作而不失去其“pythonic”风格?
a = [1, 2, 3]
for num in a:
if num == 3:
num = 2
# -> [1, 2, 3]
for i in range(len(a)):
if a[i] == 3:
a[i] = 2
# -> [1, 2, 2]
Run Code Online (Sandbox Code Playgroud) 两个都
x = 1
f"{x}"
Run Code Online (Sandbox Code Playgroud)
和
x = '1'
f"{x}"
Run Code Online (Sandbox Code Playgroud)
给
'1'
Run Code Online (Sandbox Code Playgroud)
作为输出。我如何获得"1"and"'1'"作为输出?