我觉得我在这里错过了一些明显的东西!
seq = {'a': ['1'], 'aa': ['2'], 'aaa': ['3'], 'aaaa': ['4'], 'aaaaa': ['5']}
for s in seq:
print s
Run Code Online (Sandbox Code Playgroud)
输出:
a
aa
aaaa
aaaaa
aaa
Run Code Online (Sandbox Code Playgroud)
当然它应该输出:
a
aa
aaa
aaaa
aaaaa
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?
谢谢 :)
我正在寻找一种方法,可以找到在另一个目录中创建的最新目录.我唯一的方法是os.listdir()显示其中的所有文件和目录.如何仅列出目录以及如何访问目录的属性以找出最新创建的目录?谢谢
我已经看过这篇文章和这篇文章以及其他许多文章,但还没有找到我的问题的答案,也无法弄明白.
我有一份清单.例如,它看起来像:
Dict = {'a':[1,2,3,4], 'b':[9,8,7,6], 'c':[8,5,3,2]}
Run Code Online (Sandbox Code Playgroud)
我想根据列表中的特定项返回已排序(降序/反向)的键列表.例如,我想根据每个列表中的第4项对a,b,c进行排序.
这应该返回sorted_keys = ['b','a','c']按值排序的列表[6,4,2].
合理?请帮忙......谢谢!
我有一个元组列表,每个元组包含一个日期,然后是一个数字.
days = [('04/02/15', 4.5),('03/15/15', 5.0),('04/21/15', 1.9)]
Run Code Online (Sandbox Code Playgroud)
我想按日期排序.如何将它们转换为DateTime对象或以其他方式对它们进行排序?
我想获得按其值排序的键列表,如果有任何关系,按字母顺序排序.我可以按值排序.如果是关系,我面临着问题.
对于字典:
aDict = {'a':8, 'one' : 1, 'two' : 1, 'three':2, 'c':6,'four':2,'five':1}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
sorted(aDict, key=aDict.get, reverse=True)
Run Code Online (Sandbox Code Playgroud)
这给了我:
['a', 'c', 'three', 'four', 'two', 'five', 'one']
Run Code Online (Sandbox Code Playgroud)
但我想要:
['a', 'c', 'four', 'three', 'five', 'one', 'two']
Run Code Online (Sandbox Code Playgroud) 我有个命令{'a': 2, 'b': 0, 'c': 1}。
需要按值对键进行排序,以便获得列表 ['b', 'c', 'a']
有没有简单的方法可以做到这一点?
如何在python中对元素元组进行排序,首先是基于值,然后是基于键.考虑我将用户输入作为字符串的程序.我想找出每个字符的数量,并在字符串中打印3个最常见的字符.
#input string
strr=list(raw_input())
count=dict()
#store the count of each character in dictionary
for i in range(len(strr)):
count[strr[i]]=count.get(strr[i],0)+1
#hence we can't perform sorting on dict so convert it into tuple
temp=list()
t=count.items()
for (k,v) in t:
temp.append((v,k))
temp.sort(reverse=True)
#print 3 most common element
for (v,k) in temp[:3]:
print k,v
Run Code Online (Sandbox Code Playgroud)
给予i/p -aabbbccde
上述代码的输出是:
3 b
2 c
2 a
Run Code Online (Sandbox Code Playgroud)
但我希望输出为:
3 b
2 a
2 c
Run Code Online (Sandbox Code Playgroud) 我正在阅读这篇关于排序词典的帖子.其中一个最受欢迎的答案表明:
sorted(dict1, key=dict1.get)
Run Code Online (Sandbox Code Playgroud)
虽然这似乎完全正常,但我没有得到这个key=dict1.get部分.
究竟是get什么,它做了什么?
我只熟悉get('X')从字典中提取X ...而且我在字典和stdtypes 的文档中找不到任何内容,所以任何指针都非常感谢!
NB这里是他们要说的get(),或者这是完全不同的东西?谢谢!
get(key [,default])如果key在字典中,则返回key的值,否则返回default.如果未给出default,则默认为None,因此此方法永远不会引发KeyError
试图弄清楚如何按值对字典列表进行排序,其中值以"自定义地图"列表中的字符串开头.例如,这里是要排序的数据:
'buckets': [
{
'doc_count': 23,
'key': 'Major League Stuff'
},
{
'doc_count': 23,
'key': 'Football Stuff'
},
{
'doc_count': 23,
'key': 'Football Stuff > Footballs'
},
{
'doc_count': 23,
'key': 'Football Stuff > Footballs > Pro'
},
{
'doc_count': 22,
'key': 'Football Stuff > Footballs > College'
},
{
'doc_count': 20,
'key': 'Football Stuff > Football Stuff Collections > Neat Stuff'
},
{
'doc_count': 19,
'key': 'Football Stuff > Helmets'
},
{
'doc_count': 4,
'key': 'Jewelry'
},
{
'doc_count': …Run Code Online (Sandbox Code Playgroud) 试图建立关分拣Python字典的建议在这里,我将如何去打印基于该值的绝对值排序顺序Python字典?
我试过了:
sorted(mydict, key=abs(mydict.get))
Run Code Online (Sandbox Code Playgroud)
但这会引发错误bad operand type for abs(): 'builtin_function_or_method'abs()期望一个数字,而不是一个函数.另外,abs()是函数abs的返回值,而key只是期望一个函数.
python ×10
dictionary ×7
sorting ×5
list ×3
date ×1
directory ×1
for-loop ×1
lambda ×1
python-3.x ×1
tuples ×1