variable=";CREATEDBY~string~1~~72~0~0~0~~~0;CREATEDBYNAME~string~1~~800~0~0~0~~~1;CREATEDBYYOMINAME~string~1~~800~0~0~0~~~2;CREATEDON~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~3;CREATEDONUTC~date~1~yyyy-MM-dd HH:mm:ss.SSS~26~0~0~0~~~4"
Run Code Online (Sandbox Code Playgroud)
如何拆分(;和~)之间的每个值?
结果就像 CREATEDBY,CREATEDBYNAME,CREATEDBYYOMINAME,...
我已经尝试了下面的内容,但它首次出现了.
variable[variable.find(";")+1:myString.find("~")]
Run Code Online (Sandbox Code Playgroud)
如何使用拆分获取字符串列表?
我想从文本文件中获取每个单词,并在字典中计算单词频率.
例: 'this is the textfile, and it is used to take words and count'
d = {'this': 1, 'is': 2, 'the': 1, ...}
Run Code Online (Sandbox Code Playgroud)
我不是那么远,但我只是看不出如何完成它.我的代码到目前为止:
import sys
argv = sys.argv[1]
data = open(argv)
words = data.read()
data.close()
wordfreq = {}
for i in words:
#there should be a counter and somehow it must fill the dict.
Run Code Online (Sandbox Code Playgroud) 我有一个很长的文本文件(剧本).我想把这个文本文件转换成一个列表(每个单词都是分开的),这样我以后就可以搜索它了.
我现在的代码是
file = open('screenplay.txt', 'r')
words = list(file.read().split())
print words
Run Code Online (Sandbox Code Playgroud)
我认为这可以将所有单词拆分成一个列表,但是我无法删除所有额外的东西,比如逗号和单词结尾处的句点.我也希望将大写字母设为小写(因为我希望能够以小写字母搜索并且显示大写字母和小写字母).任何帮助都会很棒:)
我正在学习Python 3.4.2中的函数和类,我从这段代码片段的输出中得到了一些偏见:
print("This program will collect your demographic information and output it")
print ("")
class Demographics: #This class contains functions to collect demographic info
def phoneFunc(): #This function will collect user's PN, including area code
phoneNum = str(input("Enter your phone number, area code first "))
phoneNumList = []
phoneNumList[:0] = phoneNum
#phoneNumList.insert(0, phoneNum) this is commented out b/c I tried this and it made the next two lines insert the dash incorrectly
phoneNumList.insert(3, '-')
phoneNumList.insert(7, '-')
print(*phoneNumList)
x = Demographics …Run Code Online (Sandbox Code Playgroud) 我有一组这样的字符串:
span-aud-result-nhd34-124
span-aud-result-jsh43-125
span-aud-result-i843-127
span-aud-result-mj43-126
Run Code Online (Sandbox Code Playgroud)
我想在以下位置提取结尾的子字符串:
例如这样的例子:
124
125
127
126
Run Code Online (Sandbox Code Playgroud)
问题是像这样的子字符串nhd34, jsh43, i843是动态的。因此,如何在之后提取结尾子字符串-
谢谢。
我有以下句子
a = "you don't need a dog"
Run Code Online (Sandbox Code Playgroud)
和一本字典
dict = {"don't": "do not" }
Run Code Online (Sandbox Code Playgroud)
但我不能使用字典来使用下面的代码映射句子中的单词:
''.join(str(dict.get(word, word)) for word in a)
Run Code Online (Sandbox Code Playgroud)
输出:
"you don't need a dog"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
你如何将数字123456789分成[1,2,3,4,5,6,7,8,9]使用Python?
我想过滤一个这样的字符串:'Hello%World'----> List = ['Hello','World']有没有内置函数?
我有一个时间和活动的元组.所以,它将类似于:
('08:01: Woke Up\n08:05: Took Shower\n08:20: Ate Breakfast\n08:45: Left for work', )
Run Code Online (Sandbox Code Playgroud)
我需要能够像以下一样单独进行每项活动和展示:
8:01 Woke Up
8:05 Took Shower
8:20 Ate Breakfast
8:45 Left for work
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一个建议在python中做到这一点的最好/最简单的方法吗?谢谢.
我正在创建一个包含文件夹中文件的列表.这些文件的名称如下:t1507859_Etappe-02-Alpe-Adria-Trail.svg.我想分割字符串以获得类似的东西:["t1507859_Etappe-", "02", "-Alpe-Adria-Trail.svg"].我想回到我从分割操作得到的列表的第二位的数字.
dirs = os.listdir (path)
[i.split('-', 2)[1] for i in l]
print dirs
Run Code Online (Sandbox Code Playgroud)
如果我将这个代码逐行解析到python shell中,它可以工作,但如果我让它作为模块运行则不行.在那里,我只是得到正常的目录列表.
输入类似于:W,79,V,84,U,63,Y,54,X,91
输出应该是: ['W', '79', 'V', '84' , 'U', '63', 'Y', '54', 'X', '91']
但我的代码充满了逗号。
a1=list(input())
print(a1)
Run Code Online (Sandbox Code Playgroud)
我的输出是 ['W', ',', '7', '9', ',', 'V', ',', '8', '4', ',', 'U', ', ', '6', '3', ',', 'Y', ',', '5', '4', ',', 'X', ',', '9', '1']
怎么去掉逗号?注意:不能使用除 input()、split()、list.append()、len(list)、range()、print() 之外的内置函数]
可能重复:将
字符串拆分为Python中的列表
我有一个包含很多部分字符串的字符串
>>> s = 'str1, str2, str3, str4'
Run Code Online (Sandbox Code Playgroud)
现在我有一个像下面这样的功能
>>> def f(*args):
print(args)
Run Code Online (Sandbox Code Playgroud)
我需要的是将我的字符串分成多个字符串,以便我的函数打印出这样的东西
>>> f(s)
('str1', 'str2', 'str3', 'str4')
Run Code Online (Sandbox Code Playgroud)
有人有想法,我怎么能这样做?
这是我搜索的内容.
>>> s = s.split(', ')
>>> f(*s)
('str1', 'str2', 'str3', 'str4')
Run Code Online (Sandbox Code Playgroud) 我有这个字符串:
var(HELLO,)|var(Hello again)| var(HOW ARE YOU?)|outV(0)|outV(1)|outV(2)|END
Run Code Online (Sandbox Code Playgroud)
我想把它分开|,但我不知道怎么做.我不希望它在白色空间分裂,只是在白色空间|.
这可能吗?
python ×13
split ×5
list ×4
string ×4
python-3.x ×3
dictionary ×2
python-2.7 ×2
comma ×1
count ×1
readlines ×1