如何访问索引本身以获取如下列表?
ints = [8, 23, 45, 12, 78]
for i in ints:
print('item #{} = {}'.format(???, i))
Run Code Online (Sandbox Code Playgroud)
当我使用循环遍历它时for,如何访问循环索引,在这种情况下从1到5?
reversePython的str对象没有内置函数.实现此方法的最佳方法是什么?
如果提供非常简洁的答案,请详细说明其效率.例如,是否将str对象转换为其他对象等.
Python是否有使用密码编码/解码字符串的内置简单方法?
像这样的东西:
>>> encode('John Doe', password = 'mypass')
'sjkl28cn2sx0'
>>> decode('sjkl28cn2sx0', password = 'mypass')
'John Doe'
Run Code Online (Sandbox Code Playgroud)
所以字符串"John Doe"被加密为'sjkl28cn2sx0'.要获取原始字符串,我将使用密钥'mypass'"解锁"该字符串,这是我的源代码中的密码.我希望这是我用密码加密/解密Word文档的方式.
我想使用这些加密的字符串作为URL参数.我的目标是混淆,而不是强大的安全性; 没有任何关键任务被编码.我意识到我可以使用数据库表来存储键和值,但我试图做到极简主义.
在列表中找到大于x的第一个索引的最Pythonic方法是什么?
例如,用
list = [0.5, 0.3, 0.9, 0.8]
Run Code Online (Sandbox Code Playgroud)
功能
f(list, 0.7)
Run Code Online (Sandbox Code Playgroud)
会回来的
2.
Run Code Online (Sandbox Code Playgroud) 我确信在Python中有一个很好的方法可以做到这一点,但我对这门语言很新,所以请原谅我,如果这很容易!
我有一个列表,我想从该列表中挑选出某些值.我想要选择的值是列表中的索引在另一个列表中指定的值.
例如:
indexes = [2, 4, 5]
main_list = [0, 1, 9, 3, 2, 6, 1, 9, 8]
Run Code Online (Sandbox Code Playgroud)
输出将是:
[9, 2, 6]
Run Code Online (Sandbox Code Playgroud)
(即main_list中索引为2,4和5的元素).
我觉得这应该是可行的,使用像列表推导这样的东西,但我无法弄清楚(特别是,我无法弄清楚如何使用列表理解时访问项目的索引).
我有一个包含字符串列表的列表看起来像
allyears
#[['1916'], ['1919'], ['1922'], ['1912'], ['1924'], ['1920']]
Run Code Online (Sandbox Code Playgroud)
我需要有这样的输出:
#[1916, 1919, 1922, 1912, 1924, 1920]
Run Code Online (Sandbox Code Playgroud)
一直在尝试这个:
for i in range(0, len(allyears)):
allyears[i] = int(allyears[i])
Run Code Online (Sandbox Code Playgroud)
但我有错误
>>> TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
Run Code Online (Sandbox Code Playgroud) 我使用全局变量,但我读过它们不是一个好的做法或 pythonic。我经常使用的函数会给出许多我需要在主函数中使用的 yes/no 变量。例如,如何在不使用全局变量的情况下编写以下代码?
def secondary_function():
global alfa_is_higher_than_12
global beta_is_higher_than_12
alfa = 12
beta = 5
if alfa > 10:
alfa_is_higher_than_12 = "yes"
else:
alfa_is_higher_than_12 = "no"
if beta > 10:
beta_is_higher_than_12 = "yes"
else:
beta_is_higher_than_12 = "no"
def main_function():
global alfa_is_higher_than_12
global beta_is_higher_than_12
secondary_function()
if alfa_is_higher_than_12=="yes":
print("alfa is higher than 12")
else:
print("alfa isn't higher than 12")
if beta_is_higher_than_12=="yes":
print("beta is higher than 12")
else:
print("beta isn't higher thant 12")
main_function()
Run Code Online (Sandbox Code Playgroud) 我想转换一个整数
my_integer=1412323
Run Code Online (Sandbox Code Playgroud)
到一个字符列表
['1','4',...]
Run Code Online (Sandbox Code Playgroud)
我能想到的最快捷方式是
list(str(my_integer))
Run Code Online (Sandbox Code Playgroud)
这样做有什么pythonic反对意见吗?
python ×8
list ×3
c# ×1
encryption ×1
function ×1
if-statement ×1
loops ×1
passwords ×1
python-3.x ×1
string ×1