小编raj*_*hon的帖子

在没有排序函数的情况下对Python中的列表进行排序

import sys\nimport pdb\n\na = [5, 2, 4, 1]\n\nfor i in range(len(a)):\n    for j in range(len(a) - 1):\n        if a[j] > a[j+1]:\n            t = a[j]\n            a[j] = a[j+1] \n            a[j] = t\n\nprint a                   \nsys.exit()\n
Run Code Online (Sandbox Code Playgroud)\n\n

我刚刚尝试了Python \xe2\x80\x93 中的C 程序,没有该sorted函数的正常排序。为什么我没有得到排序列表?

\n

python sorting list

1
推荐指数
1
解决办法
3万
查看次数

字符串中第二个重复字符的索引

我在python中尝试一个hangman代码.为了匹配单词的字符,iam使用索引函数来获取字符的位置.例如:word ='计算机'

user_input = raw_input('Enter a character :') # say 'T; is given here

if user_input in word:
                print "\nThe Character %c is present in the word \n" %user_input 
                word_dict[word.index(user_input)] = user_input

#so the output will looks like

{0: '_', 1: '_', 2: '_', 3: '_', 4: '_', 5: 'T', 6: '_', 7: '_'} 
Run Code Online (Sandbox Code Playgroud)

现在,我的问题来自于重复的角色.

# Another example 
>>> 'CARTOON'.index('O')
4
Run Code Online (Sandbox Code Playgroud)

对于第二个'O',如何获得其索引.因为我使用了这个'索引'逻辑,所以我希望继续这样做.

python string

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

标签 统计

python ×2

list ×1

sorting ×1

string ×1