如何替换列表的每个元素中的特定字符

时间:2017-09-26 10:16:32

标签: python python-3.x list

我在Python中有一个列表,其中包含每个用户配置文件发布的推文数量。

例如:

list = ['142', '1567', '153K', '32', '10', '50']

如您所见,当数字非常高(即153567)而不是实际数字时,会存储153K

使用K替换列表中每个元素中的000字符(如果存在)的最简单方法是什么?

7 个答案:

答案 0 :(得分:2)

for (myVectorIterator = vector.begin(); myVectorIterator != vector.end(); myVectorIterator++)

在您的代码中,153K不是任何类型的有效文字

我将此列表解释为字符串列表。

答案 1 :(得分:1)

使用替换功能

[i.replace('K', '000') for i in list]

然后你可以使用int

将它转换为list o integer
[int(i.replace('K', '000')) for i in list]

在这种情况下,此列表将返回:

[142, 1567, 153000, 32, 10, 50]

答案 2 :(得分:0)

只需将list comprehensionreplace一起使用即可。单线代码:

[ele.replace('K','000') for ele in l]       #l is the list

所以我们得到

IN : l = ['142', '1567', '153K', '32', '10', '50']
OUT : ['142', '1567', '153000', '32', '10', '50']

答案 3 :(得分:0)

>> list = ['142', '1567', '153K', '32', '10', '50']
>> listNoK = [item.replace('K','000') for item in list]
>> listNoK

['142', '1567', '153000', '32', '10', '50']

或者获取数字:

>>> listNoK = [int(item.replace('K','000')) for item in list]
>>> listNoK
[142, 1567, 153000, 32, 10, 50]

答案 4 :(得分:0)

解决此任务的另一种方法如下:

list = ['142', '1567', '153K', '32', '10', '50']

for index, item in enumerate(list):
    if 'K' in item:
        list[index] = item.replace('K', '000')

答案 5 :(得分:0)

list = [142, 1567, '153K', 32, 10, 50]

comp_list = [x.replace('K', '000') if type(x) is str else x for x in list]

答案 6 :(得分:0)

...
+0    ++++++++++++++++++++++++++++++++++++++++++++++++
+0    +++ Start: Launching
+0    ++++++++++++++++++++++++++++++++++++++++++++++++
+0    [Extractor] Launching 'JWrapper-JWrapper-00049034758-complete' from master folder '/Users/user/Library/Application Support/JWrapper-APPNAME (UAT)' of class 0 using JRE 'Mac64JRE'
+1    [PickFolder] Folder JWrapper-Mac64JRE-00054617449-complete.plugin matches Mac64JRE with version 54617449
+0    [PickFolder] Set latest to 54617449 JWrapper-Mac64JRE-00054617449-complete.plugin
+0    [PickFolder] Folder JWrapper-Mac64JRE-00054617449-complete matches Mac64JRE with version 54617449
+0    [Extractor] Using private JRE JWrapper-Mac64JRE-00054617449-complete.plugin
+0    [Utils] File does NOT exist: /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin/bin/APPNAME (UAT)
+0    [Extractor] Copying FILE /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin/bin/APPNAME (UAT)
+0    [Extractor] *************************** Unable to open source file for copy /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin/bin/java
+0    *******************************************
+0    Error setting app binary executable!
+0    *******************************************
+0    [Extractor] JRE LSO file is /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWApps/JRE-LastSuccessfulOptions-JWrapper-Mac64JRE-00054617449-complete.plugin
+0    [Extractor] Building classpath
+0    [Extractor] Creating launch properties
+0    [Extractor] Checking LSO file
+0    [Extractor] Reading /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWApps/JRE-LastSuccessfulOptions-JWrapper-Mac64JRE-00054617449-complete.plugin
+0    [Utils] File does NOT exist: /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWApps/JRE-LastSuccessfulOptions-JWrapper-Mac64JRE-00054617449-complete.plugin
+0    *******************************************
+0    JRE LSO file does not exist
+0    *******************************************
+0    [Extractor] Writing launch properties file
+0    [Extractor] Writing launch properties to /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-JWrapper-00049034758-complete/JWLaunchProperties-1506477183962-19
+0    [Extractor] Launch properties length is 868
+0    [Extractor] Writing to file
+1    [Extractor] Finished writing launch properties file
+0    [Extractor] Reading 0 extra args
+0    [Extractor] Read JRE compatibility class jwrapper.updater.GenericUpdaterJreVerifierLaunch
+0    [Extractor] Appending 1 command line args
+0    [Extractor] Executable: /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin/bin/APPNAME (UAT)
+0    [Extractor] Argument 0: /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin/bin/APPNAME (UAT)
+0    [Extractor] Argument 1: -cp
+0    [Extractor] Argument 2: /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-JWrapper-00049034758-complete/jwrapperlib/jwstandalonelaunch.jar
+0    [Extractor] Argument 3: jwrapper.updater.GenericUpdaterLaunch
+0    [Extractor] Argument 4: /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-JWrapper-00049034758-complete/JWLaunchProperties-1506477183962-19
+0    [Extractor] Argument 5: -psn_0_2048500
+0    [Extractor] This is a newer extractor... launchclass is 0
+0    [Extractor] JRE folder is /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin
+0    [BundleLoader] Creating jre path URL for path /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin.plugin
+0    [BundleLoader] Created NSURL
+0    [BundleLoader] Bundle is null?+0    [Extractor] Adjusted JRE folder is /Users/user/Library/Application Support/JWrapper-APPNAME (UAT)/JWrapper-Mac64JRE-00054617449-complete.plugin.plugin/Contents/Home
+0    [Extractor] Force spawn is 0 so attempting to launch via JNI
+0    ++++++++++++++++++++++++++++++++++++++++++++++++
+0    +++ Start: JNI Launch
+0    ++++++++++++++++++++++++++++++++++++++++++++++++
+0    [JNILaunch] [pid=9416] [mainThread=0]
+0    [JNILaunch] [ERROR] Could not load dynamic library!
+0    [JNILaunch] [ERROR] Unable to load JRE library!
+0    [Extractor] Return code for JNI launch was 1
+0    ------------------------------------------------
+0    --- End: JNI Launch
+0    ------------------------------------------------
+0    [Extractor] JNI launch failed. Attempting legacy spawn instead
+0    [Extractor] Attempting spawn launch
+0    ++++++++++++++++++++++++++++++++++++++++++++++++
+0    +++ Start: Spawn launch
+0    ++++++++++++++++++++++++++++++++++++++++++++++++
+0    [Utils] Closing logging file.

输出:

data = ["142", "1567", "153K", "32", "10", "50"]
for i in range(len(data)):
    data[i] = data[i].replace('K','000')
print(data)