要打印字典,我通常会这样做:
def print_row(self):
print(json.dumps(self.example_entry, indent=4, sort_keys=True))
Run Code Online (Sandbox Code Playgroud)
但是,这是我现在得到的:
>>> f.print_row()
{
"#export_date": "1475226000146",
"artist_id": "1159638168",
"artist_type_id": "1",
"is_actual_artist": "1",
"name": "\uc2e4\ubc84 \ud14c\uc774\ube14",
"view_url": "http://itunes.apple.com/artist/silbeo-teibeul/id1159638168?uo=5"
}
Run Code Online (Sandbox Code Playgroud)
如何name打印字段,因为它将在字典中打印,例如:
>>> print(f.example_entry)
{'#export_date': '1475226000146', 'artist_id': '1159638168', 'name': '?? ???', 'is_actual_artist': '1', 'view_url': 'http://itunes.apple.com/artist/silbeo-teibeul/id1159638168?uo=5', 'artist_type_id': '1'}
Run Code Online (Sandbox Code Playgroud)
注意,我也试过使用name,我认为这比上述两种选择都差.希望有一个选项,name它会给我我正在寻找的东西.
我在将一个较大的列表"拆分"成几个组合时遇到了问题.这是一个例子:
假设我有这个清单:
x = [['a','b'],['c','f'],['q','w','t']]
Run Code Online (Sandbox Code Playgroud)
我想结束
x = [['a','b'],['c','f'],['q','w'],['q','t'],['w','t']]
Run Code Online (Sandbox Code Playgroud)
基本上
['q','w','t']
Run Code Online (Sandbox Code Playgroud)
变
['q','w'],['q','t'],['w','t']
Run Code Online (Sandbox Code Playgroud)
我知道如何转换
['q','w','t']
Run Code Online (Sandbox Code Playgroud)
至
[['q','w'],['q','t'],['w','t']] #notice the extra brackets
Run Code Online (Sandbox Code Playgroud)
与itertools组合,但后来我坚持
x = [['a','b'],['c','f'],[['q','w'],['q','t'],['w','t']]] #notice the extra brackets
Run Code Online (Sandbox Code Playgroud)
这不是我想要的.
我该怎么做?
编辑:
这是"解决方案",它没有给我我想要的结果:
from itertools import combinations
x = [['a','b'],['c','f'],['q','w','t']]
new_x = []
for sublist in x:
if len(sublist) == 2:
new_x.append(sublist)
if len(sublist) > 2:
new_x.append([list(ele) for ele in (combinations(sublist,2))])
Run Code Online (Sandbox Code Playgroud)
谢谢
当我尝试运行此代码时,我得到了不正确的最大值和最小值。谁能告诉我如何修复它?我不允许使用“最大”和“最小”。
更新:我已经更新了代码,但它仍然无法正常工作。更新 2:代码现在可以使用了!十分感谢大家!
minimum=float('inf')
maximum=None
count=0
total=0
number=input ("Please enter the amount of numbers you wish to categorize: ")
while True:
num = input("Enter a number: ")
count+=1
total+=num
if num is None or num < minimum:
minimum = num
if num is None or num > maximum:
maximum = num
if count == number:
break
print "The average of your numbers is ", round ((total / count),2),"."
print 'The largest number is:', maximum,"."
print 'The smallest number is:', minimum,"."
Run Code Online (Sandbox Code Playgroud) >>> d = {'AUD': 1, 'GBP': 2}
>>> Currency.objects.filter(currency_code__in=d)
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]
>>> Currency.objects.filter(currency_code__in=d.keys())
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]
Run Code Online (Sandbox Code Playgroud)
好有意义.但:
>>> Currency.objects.filter(currency_code__in=d.keys)
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]
Run Code Online (Sandbox Code Playgroud)
WTF?为什么Django试图调用我发送的参数?这有什么特点是必要的?我当然期待不同,因为当然:
>>> d.keys
<function keys>
>>> 'AUD' in d.keys
# TypeError: argument of type 'function' is not iterable
Run Code Online (Sandbox Code Playgroud) 我想要摆脱字符串中间的字符.这些字符是=,p,, and H.由于它们不是字符串中最左边和最右边的字符,所以我无法使用strip().是否有一个函数可以删除字符串中任何位置的某个字符?
如何直接在dict_keys课堂上获得参考?目前我唯一能找到的方法是创建一个临时的dict对象并对其进行类型检查.
>>> the_class = type({}.keys())
>>> the_class
<class 'dict_keys'>
>>> the_class.__module__
'builtins'
>>> import builtins
>>> builtins.dict_keys
AttributeError: module 'builtins' has no attribute 'dict_keys'
Run Code Online (Sandbox Code Playgroud) 我有一份清单
[9,1,2,11,8]
Run Code Online (Sandbox Code Playgroud)
我需要在这个列表中打印前3个,比如
[9,11,8]
Run Code Online (Sandbox Code Playgroud)
很容易排序并获取最高值并循环遍历同一个复制列表以查找给定顺序中的顶部值但是我不应该使用新列表来执行此任务.那可能吗?
我试图使用下面的代码在嵌套列表中找到最长的字符串
table_data = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
cnt = [""]*3
for tab in range(len(table_data)):
for liel in table_data[tab]:
if len(liel) > len(cnt[tab]):
cnt[tab]=liel
print(cnt)
# ['cherries', 'Alice', 'moose']
Run Code Online (Sandbox Code Playgroud)
上面的代码返回每个列表中最长的字符串,但我认为它的长代码,有没有其他方法可以做到这一点?
期待使用列表理解或功能的任何方法
问候
# left rotate using slicing
def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]
print(arr)
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
print(arr)
Run Code Online (Sandbox Code Playgroud)
结果:
[3, 4, 5, 6, 7, 1, 2]
[1, 2, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
当我在函数外部打印数组时,它不再旋转,并保持原来的状态.有人能帮助我理解这个吗?
python ×10
list ×4
string ×2
dictionary ×1
django ×1
ieee-754 ×1
json ×1
parsing ×1
python-2.7 ×1
python-3.x ×1
unicode ×1