So'zlarni bo'shliqlar bilan o'chiring

Ro'yxatdagi barcha "iboralarni" ro'yxatdan olib tashlashni xohlayman, shuning uchun faqat so'zlar (bo'sh joylar) qolmagan. Men jangchi o'yinini qilyapman va kompyuterni tasodifiy so'zni tanlashni xohlayman. Men Python uchun yangi va kodlashni boshladim, shuning uchun kodim uchun boshqa takliflarni eshitishdan xursandman.

import random
fhand = open('common_words.txt')

words = []

for line in fhand:
    line = line.strip()
    words.append(line)

for word in words:
    if ' ' in word:
        words.remove(word)

print(words)
0

8 javoblar

str.split() dan foydalaning. Odatiy ravishda ikkala bo'shliq va yangi satrlarni ajratadi.

>>> 'some words\nsome more'.split()
['some', 'words', 'some', 'more']
>>> 'this is a sentence.'.split()
['this', 'is', 'a', 'sentence.']
>>> 'dfsonf 43 SDFd [email protected]'.split()
['dfsonf', '43', 'SDFd', '[email protected]']

Faylni an'anaviy tarzda o'qing va quyidagi tarzda ro'yxat tuzing:

words = []
with open('filename.txt','r') as file:
    words = file.read().split()

Bu yaxshi bo'lishi kerak.

0
qo'shib qo'ydi

Yuqorida aytib o'tilgandek     .Split() Boshqaruv yechim bo'lishi mumkin.

Bundan tashqari, NLTK moduli kelgusida tilni qayta ishlash vazifalari uchun foydali bo'lishi mumkin.

Umid qilamanki bu yordam!

0
qo'shib qo'ydi

Yuqorida aytib o'tilgandek     .Split() Boshqaruv yechim bo'lishi mumkin.

Bundan tashqari, NLTK moduli kelgusida tilni qayta ishlash vazifalari uchun foydali bo'lishi mumkin.

Umid qilamanki bu yordam!

0
qo'shib qo'ydi
with open( 'common_words.txt', 'r' ) as f:
    words = [ word for word in filter( lambda x: len( x ) > 0 and ' ' not in x, map( lambda x: x.strip(), f.readlines() ) ) ]

with is used because file objects are content managers. The strange list-like syntax is a list comprehension, so it builds a list from the statements inside of the brackets. map is a function with takes in an iterable, applying a provided function to each item in the iterable, placing each transformed result into a new list*. filter is function which takes in an iterable, testing each item against the provided predicate, placing each item which evaluated to True into a new list*. lambda is used to define a function (with a specific signature) in-line.

*: Haqiqiy qaytish turlari quyidagilar bilan ishlatish uchun ularni yineleyicilar kabi ishlaydigan generatorlar for loops.

0
qo'shib qo'ydi
with open( 'common_words.txt', 'r' ) as f:
    words = [ word for word in filter( lambda x: len( x ) > 0 and ' ' not in x, map( lambda x: x.strip(), f.readlines() ) ) ]

with is used because file objects are content managers. The strange list-like syntax is a list comprehension, so it builds a list from the statements inside of the brackets. map is a function with takes in an iterable, applying a provided function to each item in the iterable, placing each transformed result into a new list*. filter is function which takes in an iterable, testing each item against the provided predicate, placing each item which evaluated to True into a new list*. lambda is used to define a function (with a specific signature) in-line.

*: Haqiqiy qaytish turlari quyidagilar bilan ishlatish uchun ularni yineleyicilar kabi ishlaydigan generatorlar for loops.

0
qo'shib qo'ydi

Kassetalar ro'yxatlardan ko'ra samaraliroq. Bu erda xuddi londonda qurilgan bo'lsa, unda siz katta ishlash qobiliyatiga ega bo'lishingiz mumkin.

# Load all words
words = {}
with open('common_words.txt') as file:
    for line in file.readlines():
        line = line.strip()
        if " " not in line:
            words.add(line)
# Can be converted to one-liner using magic of Python
words = set(filter(lambda x: " " in x, map(str.strip, open('common_words.txt').readlines())))

# Get random word
import random
print(random.choice(words))
0
qo'shib qo'ydi
if ning "" satrida emas: Bu men xohlagan narsaning kaliti edi. Men to'plamlar va ro'yxatlar o'rtasidagi farqlarni ko'rib chiqishim kerak. Rahmat!
qo'shib qo'ydi muallif Lauren Gainsbrook, manba

Kassetalar ro'yxatlardan ko'ra samaraliroq. Bu erda xuddi londonda qurilgan bo'lsa, unda siz katta ishlash qobiliyatiga ega bo'lishingiz mumkin.

# Load all words
words = {}
with open('common_words.txt') as file:
    for line in file.readlines():
        line = line.strip()
        if " " not in line:
            words.add(line)
# Can be converted to one-liner using magic of Python
words = set(filter(lambda x: " " in x, map(str.strip, open('common_words.txt').readlines())))

# Get random word
import random
print(random.choice(words))
0
qo'shib qo'ydi
if ning "" satrida emas: Bu men xohlagan narsaning kaliti edi. Men to'plamlar va ro'yxatlar o'rtasidagi farqlarni ko'rib chiqishim kerak. Rahmat!
qo'shib qo'ydi muallif Lauren Gainsbrook, manba

Men sizni to'g'ri tushunganimga amin emasman, lekin split() usuli siz uchun bir narsa, deb o'ylayman:

with open('common_words.txt') as f:
    words = [line.split() for line in f]

words = [word for words in words_nested for word in words]  # flatten nested list
0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming