简体   繁体   English

将 python 列表划分为嵌套列表

[英]Divide a python list into Nested List

Hello python enthusiasts.!您好 python 爱好者! I have a text file with content like this.我有一个包含这样内容的文本文件。

Pakistan[country]\n
Karachi\n
lahore\n
islamabad\n
UAE[country]\n
dubai\n
sharjah\n
India[country]\n
goa\n
chennai\n

I read this file using this code.我使用此代码阅读了此文件。

mylist = list(open('data.txt', 'r'))

now i have all of elements in list structure, but i want to transform this into a nested list, a list structure like below.现在我拥有列表结构中的所有元素,但我想将其转换为嵌套列表,如下所示的列表结构。

[['Pakistan', 'Karachi', 'lahore', 'islamabad'],['UAE', 'dubai', 'sharjah'],['India', 'goa', 'chennai']]

Kindly help me to transform this text into above structure.请帮助我将此文本转换为上述结构。

Here you go:这里是 go:

>>> result = []
>>> for entry in mylist:
        if entry.endswith('[country]'):
            country= entry[:entry.rindex('[')]
            result.append([country])
        else:
            result[-1].append(entry)


>>> result
[['Pakistan', 'Karachi', 'lahore', 'islamabad'], ['UAE', 'dubai', 'sharjah'], ['India', 'goa', 'chennai']]

You can do something like this.你可以做这样的事情。

results = []
tmp =[]
with open("data.txt", "r") as file:
    for line in file:
        if "country" in line:
            if tmp:
                results.append(tmp)
             tmp = []
             line = line.replace("[country]", "")
        tmp.append(line.strip())
     results.append(tmp)
print(results)   
mylist = list(open('data.txt', 'r')) superlist = [] countrylist = [] for entry in mylist: if '[country]' in entry: superlist.append(countrylist) countrylist = [entry.replace('[country]', '')] else: countrylist.append(entry) if len(countrylist) > 0: superlist.append(countrylist)
temp = StringIO("""  
Pakistan[country]\n
Karachi\n
lahore\n
islamabad\n
UAE[country]\n
dubai\n
sharjah\n
India[country]\n
goa\n
chennai\n
""")


df = pd.read_csv(temp, sep='\s+', engine='python',header=None)


using re.split 使用re.split

 country_l = ' '.join(list(df[0])) [i.replace('[country]','').split() for i in re.split('\s(?=\w*\[country\])',country_l)] ##output [['Pakistan', 'Karachi', 'lahore', 'islamabad'], ['UAE', 'dubai', 'sharjah'], ['India', 'goa', 'chennai']]
my_list = list(open('data.txt', 'r'))    
j = -1
for country in my_list:
    if country.__contains__('[country]'):
        country = country[:country.find('[country]')]
        result.append([country])
        j += 1
    else:
        country = country[:country.find('\n')]
        result[j].append(country)

Disclaimer: this solution is not for the faint-hearted, and does not really prioritize readability.免责声明:此解决方案不适合胆小的人,并且并没有真正优先考虑可读性。

I'm a little intrigued that none of the currently posted answers solves this issue by .split("[country]") .我有点好奇,目前发布的答案都没有通过.split("[country]")解决这个问题。 Here is a solution by list comprehensions:这是列表推导的解决方案:

 # read file this way to preserve line structure:
 with open('data.txt', 'r') as f:
     data = f.read().strip().split("\n")

 # First: move the "[country]" tag from behind to in front of the name.
 # Also, I replace "[country]" with "*" for no other reason than that it's shorter
 data = ",".join(["*"+x.replace("[country]","") if x.endswith("[country]") else x for x in data])

 # Then, split on "[country]", that is: ",*",
 # But keep in mind that the very first country will have prefix "*", not ",*"
 data = [x.replace("*","").split(",") for x in data.split(",*")]

 print(data)
 #[['Pakistan', 'Karachi', 'lahore', 'islamabad'], ['UAE', 'dubai', 'sharjah'], ['India', 'goa', 'chennai']]

Another, much cleaner approach if you're able to change the format of your data file such that the tag [country] is set in front of the country name instead of after, then it is a little simpler, and you'll get away with a simple one-liner:另一种更简洁的方法,如果您能够更改数据文件的格式,以便将标签[country]设置在国家/地区名称之前而不是之后,那么它会更简单一些,并且您会逃脱用一个简单的单线:

 with open('data2.txt', 'r') as f:
     data = f.read().strip().split("[country]")

 data = [x.strip().replace("[country]","").split("\n") for x in data[1:]]

 print(data)
 #[['Pakistan', 'Karachi', 'lahore', 'islamabad'], ['UAE', 'dubai', 'sharjah'], ['India', 'goa', 'chennai']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM