我正在尝试从文本文件中读取一些数字并将它们转换为浮点数列表,但我尝试的任何数据似乎都能正常工作.
这是我现在的代码:
python_data = open('C:\Documents and Settings\redacted\Desktop\python_lengths.txt','r')
python_lengths = []
for line in python_data:
python_lengths.append(line.split())
python_lengths.sort()
print python_lengths
Run Code Online (Sandbox Code Playgroud)
它返回:
[['12.2'], ['26'], ['34.2'], ['5.0'], ['62'], ['62'], ['62.6']]
Run Code Online (Sandbox Code Playgroud)
(包括所有括号)
但是我不能用任何常规命令将它转换为浮点数列表,例如:
python_lengths = float(python_lengths)
Run Code Online (Sandbox Code Playgroud)
要么:
float_lengths = [map(float, x) for x in python_lengths]
Run Code Online (Sandbox Code Playgroud)
因为它似乎是嵌套的还是什么?
您将如何为包含<a>标记的以下列表正确编写jquery 代码.我有两个问题.
当我单击父列表项以显示子项时,父项将消失,并简单地替换为同一行上的缩进子项.
有些相关,我可以出于某种原因点击"1""2""3"和"4"的最后一个元素,导致列表实际上消失.我该如何解决这个问题?
HTML:
<div class="main_list">
<ul>
<li><a href="#">1.</a>
<ul>
<li><a href="#">1.1</a>
<ul>
<li><a href="#">1.1.1</a></li>
</ul>
</li>
</ul>
<li><a href="#">2.</a></li>
<li><a href="#">3.</a>
<ul>
<li><a href="#">3.1</a></li>
<li><a href="#">3.2</a></li>
</ul>
<li><a href="#">4.</a>
<ul>
<li><a href="#">4.1</a>
<ul>
<li><a href="#">4.1.1 </a></li>
<li><a href="#">4.1.2 </a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
JQUERY:
$(document).ready(function(){
// only show top level elements to begin
$("ul ul").hide();
$("li").click(function(){
$(this).children().toggle(500);
return false;
});
});
Run Code Online (Sandbox Code Playgroud) 新手在这里试图搜索另一个子列表中的一个子列表的一部分.
list_1 = [[1, 2, 9], [4, 5, 8]]
list_2 = [[1, 2, 3], [4, 5, 6], [1, 2, 5]]
for item in list_1:
for otherItem in list_2:
item[0:2] in otherItem[0:2]
Run Code Online (Sandbox Code Playgroud)
我希望这会回来
True
False
True
False
True
False
Run Code Online (Sandbox Code Playgroud)
但相反,我每次迭代都会得到假.简而言之:
list_1[0][0:2] == list_2[0][0:2] #this returns true
list_1[0][0:2] in list_2[0][0:2] #this returns false
Run Code Online (Sandbox Code Playgroud)
我想我不明白是怎么in运作的.谁能在这里教我?
我试图附加到嵌套在字典中的列表,以便我可以看到哪些字母跟随一个字母.我想在底部获得理想的结果.为什么这不匹配?
word = 'google'
word_map = {}
word_length = len(word)
last_letter = word_length - 1
for index, letter in enumerate(word):
if index < last_letter:
if letter not in word_map.keys():
word_map[letter] = list(word[index+1])
if letter in word_map.keys():
word_map[letter].append(word[index+1])
if index == last_letter:
word_map[letter] = None
print word_map
desired_result = {'g':['o', 'l'], 'o':['o', 'g'], 'l':['e'],'e':None}
print desired_result
Run Code Online (Sandbox Code Playgroud) 起初我不得不说我是AngularJS的新手,但我必须修改一个Web应用程序,即将列表嵌入另一个:
<ul class="first-level">
<li ng-repeat="item in list">{{item.parentNo1}}
<ul class="level-2">
<li ng-repat="item in list">{{item.childOfCurrentParent}}</li>
. . .
</ul>
</li>
<li ng-repeat="item in list">{{item.parentNo2}}
<ul class="level-2">
<li ng-repat="item in list">{{item.childOfCurrentParent}}</li>
. . .
</ul>
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
我收到了一个像这样的对象数组(JSON):
[{
"id": 53,
"nazwa": "Plafon Nekko",
"foto": "01/01 _ Koma Nekko.jpg",
"visible": 0,
"cat_id": 1,
"strona": 1,
"styl": "nowoczesny",
"rodzina": "Nekko",
"kod": "O2177",
"bookmark": true,
"title": "nowoczesny Nekko",
"page": 1,
"image": "/lemir/www//gazetka/01/01 _ Koma Nekko.jpg",
"width": 849,
"height": 1201,
"tags": "nowoczesny Koma O2180 Plafon Koma …Run Code Online (Sandbox Code Playgroud) 我的代码看起来如下:
from itertools import groupby
for key, group in groupby(warnstufe2, lambda x: x[0]):
for element in group:
a = element[1:4]
b = element[4:12]
c = [a,b]
print(c)
Run Code Online (Sandbox Code Playgroud)
当我打印(c)我得到这样的东西:
[(a,b,c),(d,e,f)]
[(g,h,i),(j,k,l)]
Run Code Online (Sandbox Code Playgroud)
其中a1 =(a,b,c),b1 =(d,e,f),a2 =(g,h,i),b2 =(j,k,l).当然有a3 ...和b3 ...但是,我需要这样的东西:
[(a,b,c),(d,e,f),(g,h,i),(j,k,l)]
Run Code Online (Sandbox Code Playgroud)
我已经通过c尝试了for循环:
for item in c:
list1 = []
data = list1.append(item)
Run Code Online (Sandbox Code Playgroud)
但这没有帮助,导致:
None
None
Run Code Online (Sandbox Code Playgroud)
基于以下链接:https: //mail.python.org/pipermail/tutor/2008-February/060321.html
我似乎很容易,但我是python的新手,并没有找到解决方案,尽管有很多阅读.我感谢您的帮助!
我有一个包含~300个列表的列表,但是其中一些是重复的,我想删除它们.我试过了:
cleanlist = [cleanlist.append(x) for x in oldlist if x not in cleanlist]
Run Code Online (Sandbox Code Playgroud)
但它一直RuntimeError: maximum recursion depth exceeded in comparison在向我投掷.我尝试过,sys.setrecursionlimit(1500)但没有帮助.
有什么更好的方法呢?
我有两个内容词典:
dct1 = {'NL': 7,'MC': 9, 'PG': 8}
dct2 = {'NL': 2,'MC': 10,'PG': 6}
Run Code Online (Sandbox Code Playgroud)
你可以说这些代表了一个游戏中的分数,其中字母是名称而数字是分数.两个词典之间的差异是根据标准计算它们的数字.
现在我想将字典中的内容组合成列表列表.我将简单地提供我的代码.基本上我当时所做的就是将两个词典中的内容转换为列表列表,其中:
L1 = [['NL',7],['MC',9],['PG',8]]
L2 = [['NL',2],['MC',10],['PG',6]]
Run Code Online (Sandbox Code Playgroud)
将它们转换为列表列表的代码:
L1 = []
for i, occurrences in dct1.items():
L1.append([i,occurrences])
L2 = []
for j, occurrences in dct2.items():
L2.append([j,occurrences])
Run Code Online (Sandbox Code Playgroud)
一旦我打印了两个列表,我就像上面写的一样.
但是现在,我想将它们组合成一个列表,而不是有两个不同的列表,我的输出是:
L3 = [['NL',7,2],['MC',9,10],['PG',8,6]]
Run Code Online (Sandbox Code Playgroud)
基本上单个列表不必重复两次字母,只需添加第二个数字.任何帮助深表感谢.
我有一个名为matrix包含一些行的列表.每个row字典都包含一些字典,每个字典可以包含在多行中.
我想生成一个列表dictionaries,其中包含矩阵中的所有字典,但没有重复.我已经有了解决方案,但我想使用理解.
row1 = [{'NODE':1}, {'NODE':2}, {'NODE':3}]
row2 = [{'NODE':3}, {'NODE':4}, {'NODE':5}]
row3 = [{'NODE':4}, {'NODE':6}, {'NODE':7}]
matrix = [row1, row2, row3]
dictionaries = []
for row in matrix:
for dictionary in row:
items.append(dictionary) if dictionary not in dictionaries else None
print dictionaries
[{'NODE':1}, {'NODE':2}, {'NODE':3}, {'NODE':4}, {'NODE':5}, {'NODE':6}, {'NODE':7}]
Run Code Online (Sandbox Code Playgroud)
我想要像下面这样的东西,但它不起作用,因为我在创建它时不能要求检查列表:
dictionaries = [dictionary for row in matrix for dictionary in row if dictionary not in dictionaries]
Run Code Online (Sandbox Code Playgroud)
字典键和值是原始的不可变对象,如字符串和整数.
python dictionary list-comprehension duplicates nested-lists
nested-lists ×10
python ×8
list ×5
dictionary ×3
duplicates ×2
angularjs ×1
html-lists ×1
javascript ×1
jquery ×1
json ×1
loops ×1
nested ×1
tuples ×1