如何在Python中反转字符串中的单词(不反转整个字符串)?

时间:2017-11-12 11:17:03

标签: python string

问题是这个,但我不能做字符串5反转字符串。 句子要加密如下。

•将句子作为字符串读入,称为string1。

•使用循环创建string2。它是没有空格的string1。

•string3是使用循环创建的。它包含字符串1中的所有字符以及每四个字符后的空格。

•string4是使用循环创建的。每个字符都被字母表中的下一个字符替换,空格被替换为“*”,“Z”或“z”被“A”或“a”替换

•string5是使用循环或嵌套循环(循环内循环)创建的。由“*”分隔的每组四个中的字符相反。

例如,如果string1 =“猫坐在垫子上”

library(rvest)

pg <- read_html("http://ir.las.ac.cn/handle/12502/8473/browse?type=dateissued")

html_nodes(pg, xpath=".//tr[@class='itemLine']/td[2]/span[1]/a") %>% 
  html_text()
##  [1] "Data-driven Discovery: A New Era of Exploiting the Literature and Data"                                                                                       
##  [2] "Contents Index to Volume 1"                                                                                                                                   
##  [3] "Topic Detection Based on Weak Tie Analysis: A Case Study of LIS Research"                                                                                     
##  [4] "Open Peer Review in Scientific Publishing: A Web Mining Study of <i>PeerJ</i> Authors and Reviewers"                                                          
##  [5] "Mapping Diversity of Publication Patterns in the Social Sciences and Humanities: An Approach Making Use of Fuzzy Cluster Analysis"                            
##  [6] "Under-reporting of Adverse Events in the Biomedical Literature"                                                                                               
##  [7] "Predictive Characteristics of Co-authorship Networks: Comparing the Unweighted, Weighted, and Bipartite Cases"                                                
##  [8] "International Conference on Scientometrics & Informetrics October16-20, 2017, Wuhan · China"                                                                  
##  [9] "Identification and Analysis of Multi-tasking Product Information Search Sessions with Query Logs"                                                             
## [10] "The 1<sup>st</sup> International Conference on Datadriven Knowledge Discovery: When Data Science Meets Information Science. June 19-22, 2016, Beijing · China"
## [11] "The Power-weakness Ratios (PWR) as a Journal Indicator: Testing the “Tournaments” Metaphor in Citation Impact Studies"                                        
## [12] "Document Type Profiles in <i>Nature, Science</i>, and <i>PNAS</i>: Journal and Country Level"                                                                 
## [13] "Can Automatic Classification Help to Increase Accuracy in Data Collection?"                                                                                   
## [14] "Knowledge Representation in Patient Safety Reporting: An Ontological Approach"                                                                                
## [15] "Information Science Roles in the Emerging Field of Data Science"                                                                                              
## [16] "Data Science Altmetrics"                                                                                                                                      
## [17] "Comparative Study of Trace Metrics between Bibliometrics and Patentometrics"                                                                                  
## [18] "Identifying Scientific Project-generated Data Citation from Full-text Articles: An Investigation of TCGA Data Citation"                                       
## [19] "Mining Related Articles for Automatic Journal Cataloging"                                                                                                     
## [20] "Critical Factors for Personal Cloud Storage Adoption in ChinaCritical Factors for Personal Cloud Storage Adoption in China" 

我已经这样做了,但是字符串5不起作用:

string2 = “Thecatsatonthemat”

string3 = “Thec atsa tont hema t”

string4 = “Uifd*butb*upou*ifnb*u”

string5 = “dfiU*btub*uopu*bnfi*u”

这是我在程序运行时得到的结果:

string1=str(input("Enter sentence: "))

string2=""

string3=""

string4=""

string5=""

count=0

for char in string1:

    if char==" ":

        string2=string1.replace(" ","")

    else:

        string2+=char


for char in string2:

    count+=1

    division=count%4

    if division==0:

        string3=string3+char+" "

    elif division!=0:

        string3=string3+char




for char in string3:

    newchar=""

    charnew="*"

    if char==" ":

        string4=string4+charnew

    else:

        newchar=chr(ord(char)+1)

        if newchar=="[":

            newchar="A"

        elif newchar=="{":

            newchar="a"

        string4=string4+newchar


#could not reverse the four letter sections but I reversed the whole string
rev=""        
for char in string4:
    rev=rev+char
    string5=rev[::-1]

print("string 1 = ",string1) 

print("string 2 = ",string2)

print("string 3 = ",string3)

print("string 4 = ",string4)

print("string 5 = ",string5)

2 个答案:

答案 0 :(得分:3)

拆分并加入:

s = "Hello World"

' '.join(i[::-1] for i in s.split(" "))

输出:

'olleH dlroW'

答案 1 :(得分:1)

你需要拆分单词然后反转它们

s = 'Hello World'
line = [w[::-1] for w in s.split()]

['olleH', 'dlroW']

要将其打印为字符串,请使用print ' '.join(line)