小编wim*_*wim的帖子

如何用外国字符打印字典

要打印字典,我通常会这样做:

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它会给我我正在寻找的东西.

python unicode json

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

将列表的子列表拆分为其他子列表

我在将一个较大的列表"拆分"成几个组合时遇到了问题.这是一个例子:

假设我有这个清单:

  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)

谢谢

python list

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

Python 代码中的最大值和最小值不正确

当我尝试运行此代码时,我得到了不正确的最大值和最小值。谁能告诉我如何修复它?我不允许使用“最大”和“最小”。

更新:我已经更新了代码,但它仍然无法正常工作。更新 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)

python

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

为什么Django会尝试在字段查找中自动调用参数?

>>> 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)

python django django-queryset

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

在python中删除字符串中的某些字符

我想要摆脱字符串中间的字符.这些字符是=,p,, and H.由于它们不是字符串中最左边和最右边的字符,所以我无法使用strip().是否有一个函数可以删除字符串中任何位置的某个字符?

python string python-2.7

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

我在哪里可以找到dict_keys类?

如何直接在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)

python python-3.x

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

复杂文字的虚假解析/评估

在计算复数时,python 喜欢摆弄符号。

>>> -0j
(-0-0j)
>>> (-0-0j)
0j
Run Code Online (Sandbox Code Playgroud)

为什么?

注意我在阅读这个问题时注意到了这一点

python parsing ieee-754 complex-numbers

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

在不创建新列表的情况下以相同顺序打印列表的最大值

我有一份清单

[9,1,2,11,8]
Run Code Online (Sandbox Code Playgroud)

我需要在这个列表中打印前3个,比如

[9,11,8]
Run Code Online (Sandbox Code Playgroud)

很容易排序并获取最高值并循环遍历同一个复制列表以查找给定顺序中的顶部值但是我不应该使用新列表来执行此任务.那可能吗?

python dictionary list

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

如何在这里找到最长的字符串?

我试图使用下面的代码在嵌套列表中找到最长的字符串

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)

上面的代码返回每个列表中最长的字符串,但我认为它的长代码,有没有其他方法可以做到这一点?

期待使用列表理解或功能的任何方法

问候

python string list

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

我可以不更改函数内的列表吗?

# 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 list

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