将列表列表转换为换行符列表

时间:2017-04-10 16:24:40

标签: python

我有一个列表,每个列表都是一个句子。我试图将这个列表列表转换为列表字典,其中每个句子都有一个键。我这样做的尝试给了我一个关键词,而不是每一个句子。这是我目前输入的内容。

    <div id="myCarousel" class="carousel slide carousel-fade">


    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="" contenteditable="false"></li>
        <li data-target="#myCarousel" data-slide-to="1" class="active" contenteditable="false"></li>
        <li data-target="#myCarousel" data-slide-to="2" class="" contenteditable="false"></li>
        <li data-target="#myCarousel" data-slide-to="3" class="" contenteditable="false"></li>
        <li data-target="#myCarousel" data-slide-to="4" class="" contenteditable="false"></li>

    </ol>



    <div class="carousel-inner">


       <?php


$servername = "xxxxxxxxxxxx";
$username = "xxxxxxxxxxx";
$password = "xxxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id AS id, title AS title, imagename AS imagename, description AS description, status AS status FROM uploadslide ORDER BY id";

 $result = mysqli_query($conn, $sql);

 while($row = mysqli_fetch_assoc($result)){



?>

        <div class="item" style="">
            <div class="carousel-caption">
            <?php echo '<img src="uploads/slide/' .$row['imagename']. '" alt="" class="" /> '?>
                <h4 class=""><?php echo $row['title'] ?></h4>
                <p class="">
                   <?php echo $row['description'] ?>
                </p>
            </div>
        </div>

        <?php 

}
    mysqli_close($conn);

?>


    </div> 

    <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </a>

    <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </a>


</div>

我想要这样:

{1: '[[try', 2: 'not', 3: 'become', 4: 'man', 5: 'success', 6: 'but', 7: 
'rather', 8: 'try', 9: 'become', 10: 'man', 11: 'value]', 12: '[look', 13: 
'deep', 14: 'into', 15: 'nature', 16: 'and', 17: 'then', 18: 'you', 19: 
'will', 20: 'understand', 21: 'everything', 22: 'better]', 23: '[the', 24:
'true', 25: 'sign', 26: 'intelligence', 27: 'not', 28: 'knowledge', 29:
'but', 30: 'imagination]', 31: '[we', 32: 'cannot', 33: 'solve', 34: 'our',
 35: 'problems', 36: 'with', 37: 'the', 38: 'same', 39: 'thinking', 40: 
'used', 41: 'when', 42: 'created', 43: 'them]', 44: '[weakness', 45: 
'attitude', 46: 'becomes', 47: 'weakness', 48: 'character]', 49: '["you',
 50: 'cant', 51: 'blame', 52: 'gravity', 53: 'for', 54: 'falling', 55: 
'love"]', 56: '[the', 57: 'difference', 58: 'between', 59: 'stupidity', 60:
'and', 61: 'genius', 62: 'that', 63: 'genius', 64: 'has', 65: 'its', 66: 
'limits]]'}

提前致谢!

我的列表列表如下所示:

{1:'[[try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try', 
'become', 'man', 'value]', 2: '[look', 'deep', 'into', 'nature', 'and',
'then', 'you', 'will', 'understand', 'everything', 'better]', 3:'[the', 
'true', 'sign', 'intelligence', 'not', 'knowledge', 'but', 'imagination]',
 4:'[we', 'cannot', 'solve', 'our', 'problems', 'with', 'the', 'same', 
'thinking', 'used', 'when', 'created', 'them]', 5: '[weakness', 'attitude',
'becomes', 'weakness', 'character]', 6:'["you', 'cant', 'blame', 'gravity', 
'for', 'falling', 'love"]', '7: [the', 'difference', 'between', 'stupidity',
'and', 'genius', 'that', 'genius', 'has', 'its', 'limits]]'}

6 个答案:

答案 0 :(得分:0)

试试这个:

i = "hi world\n this is python\nyo"

sentences = i.split("\n")
index = 1
my_map = {}
for sentence in sentences:
    words = sentence.split(" ")
    my_map[index] = words
    index += 1
print(my_map)

只需用你的输入替换我。这将输入除以新行字符,然后为每个句子将单词列表映射到索引。

答案 1 :(得分:0)

我认为您需要有一个整数变量,每次创建一个键时都会添加一个变量,如此

x = [['try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try','become', 'man', 'value'],
     ['look', 'deep', 'into', 'nature', 'and','then', 'you', 'will', 'understand', 'everything', 'better']]

newdct = {}
dct_key = 1
for sublist in x:
    newdct [dct_key] = sublist
    dct_key += 1
dct_key = 1

按顺序打印出列表:

for k in newdct:
    print (newdct [dct_key])
    dct_key += 1

打印[&#39;尝试&#39;,&#39;不&#39;,&#39;成为&#39;,&#39; man&#39;,&#39;成功&#39;, &#39;但&#39;,&#39;而不是&#39;,&#39;尝试&#39;,&#39;成为&#39;,&#39; man&#39;,&#39;价值& #39] [&#39; look&#39;,&#39; deep&#39;,&#39; into&#39;,&#39; nature&#39;,&#39;和&#39;,&#39;那么&#39;,&#39;你&#39;&#39;会&#39;,&#39;了解&#39;,&#39;一切&#39;,&#39;更好&#39;]

这会打印整个字典

print (newdct)

打印 {1:[&#39;尝试&#39;,&#39;不&#39;,&#39;成为&#39;,&#39; man&#39;,&#39;成功&#39;,& #39;但&#39;,&#39;而不是&#39;,&#39;尝试&#39;,&#39;成为&#39;,&#39; man&#39;,&#39;价值&# 39;],2:[&#39; look&#39;,&#39; deep&#39;,&#39; into&#39;,&#39; nature&#39;,&#39;和&#39; ;,&#39;然后&#39;,&#39;你&#39;,&#39;将&#39;,&#39;了解&#39;,&#39;所有&#39;,&#39; ;更好&#39;]}

为每个字典键创建单个字符串:

for k in newdct:
    newdct [k] = ' '.join(newdct [k])
print (newdct)

打印{1:&#39;尝试不要成为男人的成功,而是尝试成为男人的价值&#39;,2:&#39;深入了解自然,然后你会更好地理解一切&#39;}

答案 2 :(得分:0)

不考虑标点符号......我看到你在上一篇文章中处理过:

# This is whatever your list of sentences is (before its broken into words)
sentence_list = ['hey there', 'how bout that', 'lookie here']
# Make a new dict based on the size of the sentence_list
d = dict.fromkeys(range(1, len(sentence_list) + 1))
# Loop over the sentence_list to fill in the values for the dict
for ix, sentence in enumerate(sentence_list):
    # Assign each associated key to a list of words per sentence
    d[ix+1] = sentence.split()

# result is:
{1: ['hey', 'there'], 2: ['how', 'bout', 'that'], 3: ['lookie', 'here']}

答案 3 :(得分:0)

为了简化@ new_to_coding的答案,请尝试使用zip方法,在这种情况下将采用两个列表,并将相应的元素“压缩”到字典中:

>>> x = [['try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try','become', 'man', 'value'],
         ['look', 'deep', 'into', 'nature', 'and','then', 'you', 'will', 'understand', 'everything', 'better']]
>>> dict(zip(range(1, len(x) + 1), x))
{1: ['try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try', 'become', 'man', 'value'], 2: ['look', 'deep', 'into', 'nature', 'and', 'then', 'you', 'will', 'understand', 'everything', 'better']}

答案 4 :(得分:0)

虽然你的问题没有明确说明原始列表的样子,但为了回答你的问题我会假设它看起来像这样

sentence_list = [['this', 'is', 'sentence', 'one'], ['this', 'is', 'sentence', 'two']]

创建一个字典,其中键是内部列表的编号

word_dict = {}


for i in range(len(sentence_list)):
    word_dict[i] = sentence_list[i]

对print(word_dict)的调用将返回以下内容

{0: ['this', 'is', 'sentence', 'one'], 1: ['this', 'is', 'sentence', 'two']}

如果您希望字典从一开始,代码将如下所示:

word_dict = {}

for i in range(1, len(sentence_list) + 1):
    word_dict[i] = sentence_list[i - 1]

调用print(word_dict)返回以下内容:

{1: ['this', 'is', 'sentence', 'one'], 2: ['this', 'is', 'sentence', 'two']}

答案 5 :(得分:0)

看起来你没有列表清单。而是你有一个字符串列表。注意:

list_of_lists = [['hello','world'],['test','case']] 

不一样
list_of_strings = ['[hello','world]','[test','case]']

列出的答案适用于以list_of_lists风格格式化的句子,但在字符串格式列表中却不太好。我建议重做任何方法返回当前列表,以便创建一个真正的列表列表。然后返回并应用此线程中使用的方法。这可能会产生最优雅的解决方案。