我在提交之前尝试对此字符串进行urlencode.
queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
Run Code Online (Sandbox Code Playgroud) 我不明白如何通过'任意'顺序完成字典或python中的循环.
我的意思是,它是一种编程语言,所以语言中的所有内容都必须100%确定,对吗?Python必须有某种算法来决定选择字典或集合的哪一部分,第一,第二等等.
我错过了什么?
如何从一个随机对获得dict?我正在制作一个游戏,您需要猜测一个国家的首都,我需要随机出现问题.
的dict模样{'VENEZUELA':'CARACAS'}
我怎样才能做到这一点?
我正在尝试通过索引访问dict_key的元素:
test = {'foo': 'bar', 'hello': 'world'}
keys = test.keys() # dict_keys object
keys.index(0)
AttributeError: 'dict_keys' object has no attribute 'index'
Run Code Online (Sandbox Code Playgroud)
我想得到foo.
同样的:
keys[0]
TypeError: 'dict_keys' object does not support indexing
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我知道Python中的集合是无序的,但我对它们显示的"顺序"感到好奇,因为它似乎是一致的.它们似乎每次都以相同的方式乱序:
>>> set_1 = set([5, 2, 7, 2, 1, 88])
>>> set_2 = set([5, 2, 7, 2, 1, 88])
>>> set_1
set([88, 1, 2, 5, 7])
>>> set_2
set([88, 1, 2, 5, 7])
Run Code Online (Sandbox Code Playgroud)
......和另一个例子:
>>> set_3 = set('abracadabra')
>>> set_4 = set('abracadabra')
>>> set_3
set(['a', 'r', 'b', 'c', 'd'])
>>>> set_4
set(['a', 'r', 'b', 'c', 'd'])
Run Code Online (Sandbox Code Playgroud)
我只是好奇为什么会这样.有帮助吗?
是否有可能使Django的QueryDict保留原始查询字符串的排序?
>>> from django.http import QueryDict
>>> q = QueryDict(u'x=foo³&y=bar(potato),z=hello world')
>>> q.urlencode(safe='()')
u'y=bar(potato)%2Cz%3Dhello%20world&x=foo%C2%B3'
Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么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的一些数据结构使用哈希表来存储像set或的项目dictionary.所以这些对象没有顺序.但似乎对某些数字序列而言并非如此.
例如,请考虑以下示例:
>>> set([7,2,5,3,6])
set([2, 3, 5, 6, 7])
>>> set([4,5,3,0,1,2])
set([0, 1, 2, 3, 4, 5])
Run Code Online (Sandbox Code Playgroud)
但是,如果我们进行一些小改动,它就没有排序:
>>> set([8,2,5,3,6])
set([8, 2, 3, 5, 6])
Run Code Online (Sandbox Code Playgroud)
所以问题是:Python的哈希函数如何对整数序列起作用?
如果值是一个列表,是否可以按值对python字典进行排序,我希望它按该列表的第一个值排序.例如:
data = {
"Joe" : [1, "Joe", "password", "Joe@Email.com"],
"Toby" : [2, "Toby", "password", "Toby@Email.com"],
"John" : [4, "John", "password", "John@Email.com"],
"Julie" : [3, "Julie", "password", "Julie@Email.com"]
}
Run Code Online (Sandbox Code Playgroud)
我希望它是这样的:哪里'我'是关键
for i in range(len(data)):
print("UserID: ", str(data[i][0]), ". Username: ", data[i][1])
>> UserID: 1. Username: Joe
>> UserID: 2. Username: Toby
>> UserID: 3. Username: Julie
>> UserID: 4. Username: John
Run Code Online (Sandbox Code Playgroud)
非常感谢.
可能重复:
为什么python会这样排序我的字典?
为什么迭代这个词典
d = {'tors':None,
'head':None,
'armr':None,
'arml':None,
'legl':None,
'legr':None}
for k in d.keys():
print k
Run Code Online (Sandbox Code Playgroud)
将以不同的顺序输出键:
head
legl
armr
arml
tors
legr
Run Code Online (Sandbox Code Playgroud) 我用这个来制作一个多年来以某个字母开头的名字百分比图.当绘制(和打印)我的词典(letter_d)时,键是无序的,而不是像它们被添加到词典中一样顺序.有没有办法解决这个问题,我相信我会按顺序将它们添加到dict中.如果没有,我可以创建一个方法连接我的散点图的点,以模拟正确的线图?
import csv
import matplotlib.pyplot as plt
start = 1880
stop = 1890
x = []
years = range(start, stop +1)
print years
letter_d = {}
year_d = {}
alphabet = ['Z']#,'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for i in alphabet:
letter_d[i] = 0
for year in years:
filename = 'yob' + str(year) + '.txt'
z = open(filename)
year_d[int(year)] = 0
letter_d[i] = year_d
c = 0
d = 0
for line in z:
y = line.strip().split(',')
y.remove(y[1])
c += int(y[1])
if i in y[0]: …Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么python会这样排序我的字典?
我有这个完全正常工作的代码,但它的行为很奇怪:字典中的项目不是按顺序迭代,而是以某种方式随机迭代,为什么会这样?:
#!/usr/bin/python
newDict = {}
lunar = {'a':'0','b':'0','c':'0','d':'0','e':'0','f':'0'}
moon = {'a':'1','d':'1','c':'1'}
for il, jl in lunar.items():
print "lunar: " + il + "." + jl
for im, jm in moon.items():
if il == im:
newDict[il] = jm
break
else:
newDict[il] = jl
print newDict
Run Code Online (Sandbox Code Playgroud)
输出:
lunar: a.0
lunar: c.0
lunar: b.0
lunar: e.0
lunar: d.0
lunar: f.0
{'a': '1', 'c': '1', 'b': '0', 'e': '0', 'd': '1', 'f': '0'}
Run Code Online (Sandbox Code Playgroud) 所以我在python中有一个字典,其中我有一个数字用于键,其值为attatched.
myDictionary = {"0" :0, "1":0, "2":0, "3":0, "4":0, "5":0, "6":12, "7":0,"8":0,"9":0,"10":0,"11":0, "12":0,"13":12}
Run Code Online (Sandbox Code Playgroud)
我运行的doctest说:
>>> myDictionary = {"0" :0, "1":0, "2":0, "3":0, "4":0, "5":0, "6":12, "7":0,"8":0,"9":0,"10":0,"11":0, "12":0,"13":12}
>>> new_Val = 0
>>> method0 = WordClass (new_Val, myDictionary)
True
Run Code Online (Sandbox Code Playgroud)
我正在做一些调试广告,打印出我的字典,以确保我在字典中查看正确的值.当我打印出来时,它看起来像这样:
{'11': 4, '10': 4, '13': 0, '12': 4, '1': 4, '0': 4, '3': 4, '2': 4, '5': 4, '4': 4, '7': 4, '6': 0, '9': 4, '8': 4}
Run Code Online (Sandbox Code Playgroud)
它们全都乱了.默认情况下,python在存储内存时会这样做吗?如果是这样,我可以在某处查看某些文档吗?
谢谢
python ×13
dictionary ×7
python-3.x ×3
key ×2
django ×1
for-loop ×1
hashtable ×1
http ×1
matplotlib ×1
python-2.7 ×1
query-string ×1
random ×1
set ×1
url-encoding ×1