相关疑难解决方法(0)

在'for'循环中访问索引?

如何访问索引本身以获取如下列表?

ints = [8, 23, 45, 12, 78]
for i in ints:
    print('item #{} = {}'.format(???, i))
Run Code Online (Sandbox Code Playgroud)

当我使用循环遍历它时for,如何访问循环索引,在这种情况下从1到5?

python loops list

3312
推荐指数
22
解决办法
194万
查看次数

在Python中反转一个字符串

reversePython的str对象没有内置函数.实现此方法的最佳方法是什么?

如果提供非常简洁的答案,请详细说明其效率.例如,是否将str对象转换为其他对象等.

python string

1288
推荐指数
9
解决办法
123万
查看次数

根据密码编码字符串的简单方法?

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参数.我的目标是混淆,而不是强大的安全性; 没有任何关键任务被编码.我意识到我可以使用数据库表来存储键和值,但我试图做到极简主义.

python encryption passwords

109
推荐指数
12
解决办法
18万
查看次数

什么是惯用代码?

我会对一些前后c#例子,一些非惯用语和惯用语例子感兴趣.非c#示例也可以,如果他们得到了想法.谢谢.

c#

74
推荐指数
5
解决办法
2万
查看次数

第一个Python列表索引大于x?

在列表中找到大于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 list

68
推荐指数
7
解决办法
7万
查看次数

从具有特定索引的python列表中挑选项目

我确信在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的元素).

我觉得这应该是可行的,使用像列表推导这样的东西,但我无法弄清楚(特别是,我无法弄清楚如何使用列表理解时访问项目的索引).

python list-comprehension list

27
推荐指数
1
解决办法
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)

python

10
推荐指数
3
解决办法
378
查看次数

如何避免使用全局变量?

我使用全局变量,但我读过它们不是一个好的做法或 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)

python if-statement function global-variables python-3.x

4
推荐指数
2
解决办法
6979
查看次数

是否pythonic进行两个类型转换,如列表(str(my_integer))?

我想转换一个整数

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

2
推荐指数
1
解决办法
90
查看次数