How can I get only lower case words in one list and upper case words in one list , from a list using python

huangapple go评论94阅读模式
英文:

How can I get only lower case words in one list and upper case words in one list , from a list using python

问题

我使用的代码:

  1. import re
  2. import pandas as pd
  3. import csv
  4. list = ['ArvindBholaGujarat...creativedirectorSubhashGhai...director']
  5. items = []
  6. for i in list:
  7. item = i.split('...')
  8. items.append(item)
  9. items
  10. cap = []
  11. small = []
  12. for i in items:
  13. caps = re.findall('[A-Z][^A-Z]*', str(i))
  14. low = re.findall('[a-z][^a-z]*', str(i))
  15. cap.append(caps)
  16. print(caps)
  17. lower_words = []
  18. for i in cap:
  19. if str(i) == str(i).islower():
  20. lower_words.append(i)

我从代码中期望的输出是:
列表:
ArvindBholaGujarati creativedirector
SubhashGhai director

所以基本上,这里识别的模式是具有大写字母的单词放在一个列表中,而另一个列表包含所有小写字母的单词。

这就是我想要实现的内容。我已经用Python编写了所有这些代码,如果社区成员能提供帮助,我将不胜感激。

英文:

The code I have used:

  1. import re
  2. import pandas as pd
  3. import csv
  4. list=['ArvindBholaGujarat...creativedirectorSubhashGhai...director']
  5. items=[]
  6. for i in list:
  7. item=i.split('...')
  8. items.append(item)
  9. items
  10. cap=[]
  11. small=[]
  12. for i in items:
  13. caps=re.findall('[A-Z][^A-Z]*',str(i))
  14. low=re.findall('[a-z][^a-z]*',str(i))
  15. cap.append(caps)
  16. print(caps)
  17. lower_words=[]
  18. for i in cap:
  19. if str(i)==str(i).islower():
  20. lower_words.append(i)

I have tried using the split() in python and using the islower() and isupper() in python but the required data is not coming.

**The output I expect from the code is:
List:
ArvindBholaGujarati creativedirector
SubhashGhai director
**

So basically, the pattern identified here is words with captial letters are put in one list whereas the other list contains all the lower lettered words.

This is what I would like to achieve.
I have written all this code in python and would like to get the help needed if any community member can help.

答案1

得分: 1

import re
import pandas as pd

names = 'ArvindBholaGujarat...creativedirectorSubhashGhai...director'

items = names.split('...')
print(items)

cap = []
small = []
for i in items:
caps = re.findall('[A-Z][a-z]*', str(i))
if caps:
cap.extend(caps)

  1. low = re.findall('^[a-z]+', str(i))
  2. if low:
  3. small.extend(low)

print(f'大写开头的名字有 {cap},小写的单词有 {small}')

英文:

I think this is what you're looking for

  1. import re
  2. import pandas as pd
  3. names = 'ArvindBholaGujarat...creativedirectorSubhashGhai...director'
  4. items = names.split('...')
  5. print(items)
  6. cap = []
  7. small = []
  8. for i in items:
  9. caps = re.findall('[A-Z][a-z]*', str(i))
  10. if caps:
  11. cap.extend(caps)
  12. low = re.findall('^[a-z]+', str(i))
  13. if low:
  14. small.extend(low)
  15. print(f'Caps are {cap}, and lowers are {small}')

Output:

  1. ['ArvindBholaGujarat', 'creativedirectorSubhashGhai', 'director']
  2. Caps are ['Arvind', 'Bhola', 'Gujarat', 'Subhash', 'Ghai'], and lowers are ['creativedirector', 'director']

In caps list, there are names starting with capital letters and in lowers words without any capital letters.

答案2

得分: 0

你可以使用re.findall和交替模式,首先是完全小写的单词,然后是以大写字母开头的单词(用管道操作符连接)。然后,你可以使用输出单词的第一个字母来确定要将其添加到哪个列表中,具体取决于它是大写字母单词还是全小写单词。

  1. import re
  2. data = ['ArvindBholaGujarat...creativedirectorSubhashGhai...director']
  3. cap, small = lists = [[], []]
  4. for item in data:
  5. for word in re.findall(r'[a-z]+|[A-Z]\w*', item):
  6. lists[word[:1].islower()].append(word)

输出:

  1. print(small)
  2. ['creativedirector', 'director']
  3. print(cap)
  4. ['ArvindBholaGujarat', 'SubhashGhai']
英文:

You could use re.findall with alternate patterns, first a fully lowercase word and then a word starting with uppercase letter (joined with a pipe operator). You can then use the first letter of the output word to determine which list to add it to depending on whether it is a capitalized word or all lowercase.

  1. import re
  2. data = ['ArvindBholaGujarat...creativedirectorSubhashGhai...director']
  3. cap,small = lists = [[],[]]
  4. for item in data:
  5. for word in re.findall(r'[a-z]+|[A-Z]\w*',item):
  6. lists[word[:1].islower()].append(word)

output:

  1. print(small)
  2. ['creativedirector', 'director']
  3. print(cap)
  4. ['ArvindBholaGujarat', 'SubhashGhai']

huangapple
  • 本文由 发表于 2023年7月10日 14:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定