相关疑难解决方法(0)

python字典中循环(for循环)的顺序是什么

可能重复:
为什么python会这样排序我的字典?

我对以下输出有点困惑.我不明白执行循环的顺序.

domains = { "de": "Germany", "sk": "Slovakia", "hu": "Hungary",
    "us": "United States", "no": "Norway"  }

for key in domains:
    print key
Run Code Online (Sandbox Code Playgroud)

这里的输出是

sk
de
no
us
hu
Run Code Online (Sandbox Code Playgroud)

但不是

de
sk
hu
us
no
Run Code Online (Sandbox Code Playgroud)

同样,这里

num = {1:"one",4:"two",23:"three",10:"four"}
for key in num:
    print key
output is
1
10
4
23
Run Code Online (Sandbox Code Playgroud)

但不是

1
4
23
10
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

python dictionary for-loop

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

名称及其参考值如何相关?

阅读Charles Dierbach撰写的"使用Python的计算机科学导论 "一书,我得到一个问题:

  • 在第210页中,他说"引用是一个引用或"指向"另一个实体的位置的值.变量的引用值可以通过内置函数id来确定."

所以引用只是对象内存中的地址.

现在,如果我没有弄错的话,当我创建一个变量时:

>>> a = 3
Run Code Online (Sandbox Code Playgroud)

名称a由Python在命名空间中自动放置.

如果我做:

>>> id(a)
4548344816
Run Code Online (Sandbox Code Playgroud)

我得到了对象3的内存地址,即a引用的对象.

所以我想知道的是,名称a参考值是如何相关的.

我猜测,当名称放入命名空间时,它包含两件事:

  1. 这个名字本身.

  2. 基准值(对象的ID)

像一对可能:a:reference value

如果是这样,是否有一些instrospection工具来查看命名空间中的条目是什么样的?

python

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

我可以相信每次迭代时dict的顺序保持不变吗?

我有以下三个字符串(它们独立存在,但为方便起见,这里一起显示):

from mx2.x.org (mx2.x.org. [198.186.238.144])
            by mx.google.com with ESMTPS id g34si6312040qgg.122.2015.04.22.14.49.15
            (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
            Wed, 22 Apr 2015 14:49:16 -0700 (PDT)

from HQPAMAIL08.x.org (10.64.17.33) by HQPAMAIL13.x.x.org
 (10.34.25.11) with Microsoft SMTP Server (TLS) id 14.2.347.0; Wed, 22 Apr
 2015 17:49:13 -0400

from HQPAMAIL13.x.org ([fe80::7844:1f34:e8b2:e526]) by
 HQPAMAIL08.iadb.org ([fe80::20b5:b1cb:9c01:aa86%18]) with mapi id
 14.02.0387.000; Wed, 22 Apr 2015 17:49:12 -0400
Run Code Online (Sandbox Code Playgroud)

我希望根据字符串的反转(从下到上)顺序填充一些带有某些值的字典.具体来说,对于每个字符串,我将IP地址提取为排序索引,然后将完整字符串作为值.

鉴于顺序很重要,我决定使用列表,并且最初做了类似的事情(伪代码,带有上面的一堆文本):

IPs =[]
fullStrings =[]
for string in strings:
    IPs.append[$theIpAddressFoundInTheString]
    fullstrings.append[$theWholeString]
Run Code Online (Sandbox Code Playgroud)

产生以下两个列表(再次,只是一个例子):

IPs ['198.186.238.144', '10.64.17.33', 'fe80::7844:1f34:e8b2:e526']

fullstrings ['from mx2.x.org (mx2.x.org. [198.186.238.144])
                by mx.google.com …
Run Code Online (Sandbox Code Playgroud)

python iteration dictionary list

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

NetworkX随机排列节点顺序

我是编程的初学者,现在是我的新手,所以您好!

我在networkX中的节点顺序有问题。这段代码:

letters = []
G = nx.Graph()
for i in range(nodesNum):
    letter = ascii_lowercase[i]
    letters.append(letter)
    print letters

G.add_nodes_from(letters)
print "G.nodes  = ", (G.nodes())
Run Code Online (Sandbox Code Playgroud)

返回此:

['a']
['a', 'b']
['a', 'b', 'c']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] …
Run Code Online (Sandbox Code Playgroud)

python graph nodes networkx python-2.7

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

IndexError:无法将“int”放入索引大小的整数中

所以我试图让我的程序从文本文件中打印出每个单词和标点符号的索引(当它出现时)。我已经完成了那部分。- 但问题是当我尝试使用这些索引位置重新创建带有标点符号的原始文本时。这是我的代码:

with open('newfiles.txt') as f:
    s = f.read()
import re
#Splitting string into a list using regex and a capturing group:
matches = [x.strip() for x in re.split("([a-zA-Z]+)", s) if x not in ['',' ']]
print (matches)
d = {} 
i = 1
list_with_positions = []
# the dictionary entries:
for match in matches:
    if match not in d.keys():
        d[match] = i
        i+=1
    list_with_positions.append(d[match])

print (list_with_positions)
file = open("newfiletwo.txt","w")
file.write (''.join(str(e) for e in list_with_positions))
file.close()
file = open("newfilethree.txt","w") …
Run Code Online (Sandbox Code Playgroud)

python runtime-error list append indexof

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

在Python中腌制字典

我可以期望同一个pickled dict的字符串表示在同一个Python版本的不同机器/运行中是一致的吗?在同一台机器上运行的范围?

例如

# Python 2.7

import pickle
initial = pickle.dumps({'a': 1, 'b': 2})
for _ in xrange(1000**2):
    assert pickle.dumps({'a': 1, 'b': 2}) == initial
Run Code Online (Sandbox Code Playgroud)

它取决于我的dict对象的实际结构(嵌套值等)吗?

UPD:问题是 - 无论我的dict对象是什么样的(什么键/值等),我实际上无法在一次运行(Python 2.7)的范围内使上面的代码失败

python pickle python-2.7

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

如果没有定义顺序,python 如何迭代集合?

所以我注意到我们在 python 中说集合没有顺序或排列,尽管您当然可以对集合生成的列表进行排序。

所以我想知道如何在 python 中定义集合的迭代。它是否只是遵循排序的列表顺序,或者是否有其他一些可能在某个时候突然出现的枪?

谢谢。

python set

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

如何使字典列表中的值唯一?

我在Python中有一个字典列表,如下所示:

d = [{feature_a:1, feature_b:'Jul', feature_c:100}, {feature_a:2, feature_b:'Jul', feature_c:150}, {feature_a:1, feature_b:'Mar', feature_c:110}, ...]
Run Code Online (Sandbox Code Playgroud)

我想实现的是保持feature_a,_b_c独特的.

例如,如果我们有3项具有相同feature_a_b,但有3个不同的值feature_c 100,100,150,则操作之后,它应该是100150.

我怎样才能做到这一点?

================================================== ==============更新:

好的,感谢Anand的出色答案,它完美无缺.但是,我还有一个问题.

假设我们有一个新的feature_d,字典看起来像:

d = [{feature_a:1, feature_b:'Jul', feature_c:100, feature_d:'A'}, {feature_a:2, feature_b:'Jul', feature_c:150, feature_d: 'B'}, {feature_a:1, feature_b:'Mar', feature_c:110, feature_d:'F'}, ...]
Run Code Online (Sandbox Code Playgroud)

我只想重复数据删除feature_a,_b并且_c,但是离开feature_d了.我怎样才能做到这一点?

非常感谢.

python unique

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

dict.values() 是否按顺序返回?

dict_mark = {'Wang': 'C', 'Li': 'B', 'Ma': 'A'}
s = ''
for c in dict_mark.values():
    s += c
print(s)
Run Code Online (Sandbox Code Playgroud)

字典是无序的,那么为什么dict_mark.values()总是返回这个值序列呢'C' 'B' 'A'

为什么不'B' 'A' 'C'或者'A' 'B' 'C'

python dictionary

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

如果集合是无序的,为什么python集以"相同"的顺序显示?

我首先看一下Python wikibook中的python语言.

对于集合,提到以下内容 - We can also have a loop move over each of the items in a set. However, since sets are unordered, it is undefined which order the iteration will follow.

并给出了代码示例

s = set("blerg")

for letter in s:
     print letter
Run Code Online (Sandbox Code Playgroud)

输出:

 r b e l g
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,无论我运行多少次,我都会以相同的顺序得到结果.如果集合是无序的并且迭代顺序未定义,为什么它以相同的顺序返回集合?订单的基础是什么?

[ PS:对不起,如果我误解了一些非常基本的东西.我是一个蟒蛇新手]

python set python-internals

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