相关疑难解决方法(0)

何时在Python中使用"while"或"for"

当我在Python中使用while循环或for循环时,我发现了问题.看起来人们更喜欢使用for循环(更少的代码行?).是否有任何具体情况我应该使用其中一种?这是个人偏好的问题吗?到目前为止我读过的代码让我觉得它们之间存在很大差异.

python loops

32
推荐指数
5
解决办法
7万
查看次数

Python - 将一串数字转换为int列表

我有一串数字,如:

example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
Run Code Online (Sandbox Code Playgroud)

我想将其转换为列表:

example_list = [0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
Run Code Online (Sandbox Code Playgroud)

我尝试过类似的东西:

for i in example_string:
    example_list.append(int(example_string[i]))
Run Code Online (Sandbox Code Playgroud)

但这显然不起作用,因为字符串包含空格和逗号.但是,删除它们不是一种选择,因为像"19"这样的数字会被转换为1和9.你能帮我解决这个问题吗?

python string integer list

25
推荐指数
5
解决办法
10万
查看次数

在for循环中列出append()

在Python中,尝试使用循环对列表执行最基本的追加功能:不确定我在这里缺少的内容:

a=[]
for i in range(5):    
    a=a.append(i)
a
Run Code Online (Sandbox Code Playgroud)

收益: 'NoneType' object has no attribute 'append'

python for-loop append

23
推荐指数
3
解决办法
16万
查看次数

在python的列表中连接元组的元素

我有一个元组列表,其中包含字符串例如:

[('this', 'is', 'a', 'foo', 'bar', 'sentences')
('is', 'a', 'foo', 'bar', 'sentences', 'and')
('a', 'foo', 'bar', 'sentences', 'and', 'i')
('foo', 'bar', 'sentences', 'and', 'i', 'want')
('bar', 'sentences', 'and', 'i', 'want', 'to')
('sentences', 'and', 'i', 'want', 'to', 'ngramize')
('and', 'i', 'want', 'to', 'ngramize', 'it')]
Run Code Online (Sandbox Code Playgroud)

现在我希望连接一个元组中的每个字符串以创建一个空格分隔字符串列表.我使用以下方法:

NewData=[]
for grams in sixgrams:
       NewData.append( (''.join([w+' ' for w in grams])).strip())
Run Code Online (Sandbox Code Playgroud)

哪个工作得很好.

但是,我拥有的列表有超过一百万个元组.所以我的问题是这种方法足够有效还是有更好的方法来做到这一点.谢谢.

python string tuples list concatenation

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

Python列表理解 - 访问最后创建的元素?

是否可以访问列表推导中生成的前一个元素.

我正在研究一些玩具加密的东西.将密钥作为任意大整数,初始化值和元素列表作为要加密的消息.我需要使用先前的加密元素和密钥对每个元素进行xor.以下循环可以.

previous = initialization_value
cipher = []
for element in message:
    previous = element ^ previous ^ key
    cipher.append(previous)
Run Code Online (Sandbox Code Playgroud)

我觉得应该可以将其转换为列表理解,但我不确定如何处理初始值或访问生成的先前值.是否有可能,如果是这样,理解力是什么?

python

16
推荐指数
3
解决办法
7859
查看次数

在Python中获取字符串及其子字符串的所有组合

我已经看到很多关于获取所有可能的子串(即,相邻的字符集)的问题,但没有关于生成所有可能的字符串,包括其子串的组合.

例如,让:

x = 'abc'
Run Code Online (Sandbox Code Playgroud)

我希望输出类似于:

['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

重点是我们可以删除原始字符串中不相邻的多个字符(以及相邻的字符).

这是我到目前为止所尝试的:

def return_substrings(input_string):
    length = len(input_string)
    return [input_string[i:j + 1] for i in range(length) for j in range(i, length)]

print(return_substrings('abc'))
Run Code Online (Sandbox Code Playgroud)

但是,这只会从原始字符串中删除相邻字符串集,并且不会返回'ac'上面示例中的元素.

另一个例子是,如果我们使用字符串'abcde',输出列表应该包括的内容'ace','bd'等等.

python string

14
推荐指数
2
解决办法
5778
查看次数

将字符串列表转换为整数列表

如何将空格分隔的整数输入转换为整数列表?

输入示例:

list1 = list(input("Enter the unfriendly numbers: "))
Run Code Online (Sandbox Code Playgroud)

转换示例:

['1', '2', '3', '4', '5']  to  [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

python integer input list

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

将列表转换为小写

我想在input.txt中获取这一列的单词:

Suzuki music
Chinese music
Conservatory
Blue grass
Rock n roll
Rhythm
Composition
Contra
Instruments 
Run Code Online (Sandbox Code Playgroud)

进入这种格式:

"suzuki music", "chinese music", "conservatory music", "blue grass", "rock n roll", "rhythm"...
Run Code Online (Sandbox Code Playgroud)

这段代码:

with open ('artsplus_stuff.txt', 'r') as f:
    list.append(", ".join(['%s' % row for row in f.read().splitlines()]))
    for item in list:
        item.lower()

print list
Run Code Online (Sandbox Code Playgroud)

返回一个列表,但第一个字母大写.

['铃木音乐,中国音乐,音乐学院,蓝草,摇滚,节奏,作曲,对比,乐器']

如何将所有物品放低?

谢谢!


答案不在此列表中:

Chess
Guitar
Woodworking
Gardening
Car_restoration
Metalworking
Marksman
Camping
Backpacking_(wilderness)
Hunting
Fishing
Whittling
Geocaching
Sports
Model_Building
Leatherworking
Bowling
Archery
Hiking
Connoisseur
Photography
Pool_(cue_sports)
Mountaineering
Cooking
Blacksmith …
Run Code Online (Sandbox Code Playgroud)

python

12
推荐指数
2
解决办法
5万
查看次数

在句子列表中标记单词Python

我目前有一个文件,其中包含一个看起来像的列表

example = ['Mary had a little lamb' , 
           'Jack went up the hill' , 
           'Jill followed suit' ,    
           'i woke up suddenly' ,
           'it was a really bad dream...']
Run Code Online (Sandbox Code Playgroud)

"example"是这样的句子列表,我希望输出看起来像:

mod_example = ["'Mary' 'had' 'a' 'little' 'lamb'" , 'Jack' 'went' 'up' 'the' 'hill' ....] 等等.我需要将每个单词标记为单独的句子,以便我可以将mod_example(一次使用for循环)的句子中的每个单词与参考句子进行比较.

我试过这个:

for sentence in example:
    text3 = sentence.split()
    print text3 
Run Code Online (Sandbox Code Playgroud)

得到了以下输出:

['it', 'was', 'a', 'really', 'bad', 'dream...']
Run Code Online (Sandbox Code Playgroud)

我如何为所有句子得到这个? 它会一直覆盖.是的,还要提一下我的方法是否正确?这应该是一个单词列表,标记为tokenized ..谢谢

text nltk python-2.7

11
推荐指数
2
解决办法
5万
查看次数

Python - 函数输出?

我有一个非常基本的问题.

假设我调用一个函数,例如,

def foo():
    x = 'hello world'
Run Code Online (Sandbox Code Playgroud)

如何让函数以这样的方式返回x,我可以将它用作另一个函数的输入或者在程序体内使用变量?

当我使用return并在另一个函数中调用该变量时,我得到一个NameError.

python function

10
推荐指数
2
解决办法
7万
查看次数

标签 统计

python ×9

list ×3

string ×3

integer ×2

append ×1

concatenation ×1

for-loop ×1

function ×1

input ×1

loops ×1

nltk ×1

python-2.7 ×1

text ×1

tuples ×1