我对Python中的字典有疑问.
这里是:
我有一个像这样的字典 dict = { 'abc':'a', 'cdf':'b', 'gh':'a', 'fh':'g', 'hfz':'g' }
现在我希望以相同的值获取所有Key-Elements并将其保存在新的dict中.
新的Dict应该是这样的:
new_dict = { 'b':('cdf'), 'a':('abc','gh'), 'g':('fh','hfz')}
我有一个问题是在动态元组列表中获得最高值.
List可能如下所示:
adymlist = [[('name1',1)],[('name2',2),('name3',1), ...('name10', 20)], ...,[('name m',int),..]]
Run Code Online (Sandbox Code Playgroud)
现在我遍历List以获得最高值(整数):
total = {}
y=0
while y < len(adymlist):
if len(adymlist) == 1:
#has the List only 1 Element -> save it in total
total[adymlist[y][0][0]] = adymlist[y][0][1]
y += 1
else:
# here is the problem
# iterate through each lists to get the highest Value
# and you dont know how long this list can be
# safe the highest Value in total f.e. total = {'name1':1,'name10':20, ..}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多以获得最大值,但我发现我的问题没有结论.我知道我必须遍历列表中的每个元组并将其与下一个元组进行比较,但它不知道如何正确编码.
我也可以使用该函数, …
我想要做的是将文本分成他的终极元素.
例如:
from nltk.tokenize import *
txt = "A sample sentences with digits like 2.119,99 or 2,99 are awesome."
regexp_tokenize(txt, pattern='(?:(?!\d)\w)+|\S+')
['A','sample','sentences','with','digits','like','2.199,99','or','2,99','are','awesome','.']
Run Code Online (Sandbox Code Playgroud)
你可以看到它工作正常.我的问题是:如果数字位于文本的末尾会发生什么?
txt = "Today it's 07.May 2011. Or 2.999."
regexp_tokenize(txt, pattern='(?:(?!\d)\w)+|\S+')
['Today', 'it', "'s", '07.May', '2011.', 'Or', '2.999.']
Run Code Online (Sandbox Code Playgroud)
结果应该是:['今天','它',''s','07.可能','2011','.','或','2.999','.']
我需要做些什么来获得上面的结果?
我有一个多维数组,如下所示:
$array = (
[0] => array (
['WS'] => array(
[id] => 2,
[name] => 'hello'
)
)
),
[1] => array (
['SS'] => array(
[id] => 1,
[name] => 'hello2'
)
)
),
[2] => array (
['WS'] => array(
[id] => 5,
[name] => 'helloAGAIN'
)
)
)
Run Code Online (Sandbox Code Playgroud)
如您所见,$ array [0]和$ array [2]具有相同的密钥[WS].我需要一个函数来找到那些"相同的键".然后我将这两个数组合并为一个.FE
$array =
(
[0] => array
(
['WS'] => array
(
[0] => array
(
[id] => 2,
[name] => 'hello'
),
[1] …Run Code Online (Sandbox Code Playgroud) 我很讨厌Django.我编写了一个正常工作的Python函数.出于可视化的原因,我决定制作一个网页来展示我的功能.
我在我的函数中添加了一些代码:
txt_len = 0 (line 1)
if text:
txt_len=len(text)
return txt_len
Run Code Online (Sandbox Code Playgroud)
*这只是一个例子.
但由于某种原因,我得到一个IndentationError,第1行意外缩进
为什么?无法得到它.
更新:
我在某些代码行之间有一些空格/制表符.所以我修复了它,现在它可以工作了.